当我按下向前箭头键时,播放器会向左移动

When I press the arrow key forward player goes to the left

所以我做了这个脚本

public float walkSpeed = 2;
public float runSpeed = 6;
public float turnSmoothTime = 0.2f;
float turnSmoothVelocity;
public float speedSmoothTime = 0.1f;
float speedSmoothVelocity;
float currentSpeed;
Animator animator;

void Start () {
    animator = GetComponent<Animator> ();
}

void Update () {
    Vector2 input = new Vector2 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"));
    Vector2 inputDir = input.normalized;

    if (inputDir != Vector2.zero) {
        float targetRotation = Mathf.Atan2 (inputDir.x, inputDir.y) * Mathf.Rad2Deg;

        transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, turnSmoothTime);
    }

    bool running = Input.GetKey (KeyCode.LeftShift);

    float targetSpeed = ((running) ? runSpeed : walkSpeed) * inputDir.magnitude;

    currentSpeed = Mathf.SmoothDamp (currentSpeed, targetSpeed, ref speedSmoothVelocity, speedSmoothTime);

    transform.Translate (transform.forward * currentSpeed * Time.deltaTime, Space.World);

    float animationSpeedPercent = ((running) ? 1 : .5f) * inputDir.magnitude;

    animator.SetFloat ("speedPercent", animationSpeedPercent, speedSmoothTime, Time.deltaTime);
}

现在的问题是,每当我用我的键向前或向左或向右单击时,无论如何角色都与我实际尝试去的方向成 90 度角。我试过统一旋转角色和制作它的 3d 建模程序,但仍然无法正常工作。我已经尝试解决这个问题一段时间了,请帮忙。这是一个示例视频:https://streamable.com/u7i0m 每次我单击一个键时,它都会朝与预期相反的方向移动。

你的 Atan2 倒退了。应该是 Mathf.Atan2(inputDir.y, inputDir.x).

根据播放器的设置方式,您可能还需要偏移 + 或 - 90 度。

Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg + 90;

是的,看起来你的模特脸不是变换的正向方向。将偏移量添加到您的代码或创建一个子对象来保存模型并将其旋转所需的偏移量。如果您自己创建了模型,您应该修复它并记住始终创建面向那个方向的模型。