我正在为一个非营利组织的游戏工作,我是该组织的志愿者,我试图让人物在按下WASD或箭头键时旋转90度。我已经尝试了各种方法,但所有我找到的解决方案都过时了,因为Unity最近更新了其运动系统。如果有人能帮助我,我将非常感谢。以下是我的测试代码,它包含运动和旋转。

[SerializeField]
private float rotationOffset;

private float speed = 3f;
private Rigidbody2D rb;
private Vector2 moveInput;
private Vector3 direction;

// Start是MonoBehaviour创建后第一次更新执行之前被调用
void Start()
{
    rb = GetComponent<Rigidbody2D>();
}

void Update()
{
    rb.linearVelocity = moveInput * speed;
    if (direction == Vector3.forward)
    {
        rotationOffset = 0f;
        transform.rotation = Quaternion.Euler(0, 0, rotationOffset);
        //transform.eulerAngles = new Vector3(0f, 0f, rotationOffset);
        rb.MoveRotation(rotationOffset);
    }
    else if (direction == Vector3.back)
    {
        rotationOffset = 180f;
        transform.rotation = Quaternion.Euler(0, 0, rotationOffset);
        //transform.eulerAngles = new Vector3(0f, 0f, rotationOffset);
        rb.MoveRotation(rotationOffset);
    }
    else if (direction == Vector3.left)
    {
        rotationOffset = 90f;
        transform.rotation = Quaternion.Euler(0, 0, rotationOffset);
        //transform.eulerAngles = new Vector3(0f, 0f, rotationOffset);
        rb.MoveRotation(rotationOffset);
    }
    else if (direction == Vector3.right)
    {
        rotationOffset = -90f;
        transform.rotation = Quaternion.Euler(0, 0, rotationOffset);
        //transform.eulerAngles = new Vector3(0f, 0f, rotationOffset);
        rb.MoveRotation(rotationOffset);
    }
}

public void Move(InputAction.CallbackContext context)
{
    moveInput = context.ReadValue<Vector2>();
}

public void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.gameObject.CompareTag("Wall"))
    {
        Debug.Log("碰撞了墙");
    }
    else if (collision.gameObject.CompareTag("Food"))
    {
        Debug.Log("碰撞了食物");
    }
}

如果有人有任何想法可以解决这个问题,我将非常感谢,因为我感觉自己快要失去理智了,我不知道从哪里开始。