Tra*_*fel 3 c# unity-game-engine
如何在不限制任何值或使用“.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;
}
Run Code Online (Sandbox Code Playgroud)
在这里,您应该限制输入 Vector2 的幅度。例如来自 Unity API 的Vector2.ClampMagnitude()。这将保持输入非二进制并防止对角线变得比纯水平/垂直输入更大。
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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1363 次 |
| 最近记录: |