gpt4 book ai didi

c# - 对角线速度太快

转载 作者:行者123 更新时间:2023-12-01 08:43:15 25 4
gpt4 key购买 nike

如何在不限制任何值或使用“.normaized”的情况下保持对角线速度与水平和垂直速度相同。我试图标准化这些值,但我丢失了 1 到 0 之间的操纵杆值。这是我的代码:

void ListenInput()
{
Vector3 rightDirection = camera.right;
Vector3 frontDirection = camera.GetForwardFromAngleY();

move = new Vector2(
Input.GetAxis("Horizontal"),
Input.GetAxis("Vertical")
);

MoveCharacter(rightDirection * move.x);
MoveCharacter(frontDirection * move.y);
}

void MoveCharacter(Vector3 velocity)
{
transform.position += velocity * Time.deltaTime * runningSpeed;
}

最佳答案

在这里,您应该钳制输入 Vector2 的幅度。例如 Vector2.ClampMagnitude()来自 Unity API。这将保持输入非二进制并防止对角线变得大于纯水平/垂直输入。

void ListenInput()
{
Vector3 rightDirection = camera.right;
Vector3 frontDirection = camera.GetForwardFromAngleY();

move = new Vector2(
Input.GetAxis("Horizontal"),
Input.GetAxis("Vertical")
);

move = Vector2.ClampMagnitude(move, 1f);

MoveCharacter(rightDirection * move.x);
MoveCharacter(frontDirection * move.y);
}

void MoveCharacter(Vector3 velocity)
{
transform.position += velocity * Time.deltaTime * runningSpeed;
}

关于c# - 对角线速度太快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60190028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com