【问题标题】:Contoller script doesn't change direction when the character looks around当角色环顾四周时,控制器脚本不会改变方向
【发布时间】:2019-09-20 01:06:16
【问题描述】:

我正在使用 Unity 官方网站上的控制器脚本来移动我的角色。我还使用脚本来使用鼠标转动相机。一切正常,直到角色环顾四周并面向不同的方向。然后 WASD 控件根据原始方向移动它们。例如,如果我转 180 度,W 将我向后移动,S 将我向前移动。

我尝试使用 transform.forward 来解决这个问题,但我真的不知道我在做什么。

运动脚本:

CharacterController characterController;

public float speed = 6.0f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;

private Vector3 moveDirection = Vector3.zero;

void Start()
{
    characterController = GetComponent<CharacterController>();
}

void Update()
{
    if (characterController.isGrounded)
    {
        // We are grounded, so recalculate
        // move direction directly from axes

        moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
        moveDirection *= speed;

        if (Input.GetButton("Jump"))
        {
            moveDirection.y = jumpSpeed;
        }
    }

    // Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
    // when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
    // as an acceleration (ms^-2)
    moveDirection.y -= gravity * Time.deltaTime;

    // Move the controller
    characterController.Move(moveDirection * Time.deltaTime);
}

感谢您的帮助:)

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    您可以使用Transform.TransformDirection 将本地方向转换为世界方向。用你想要移动的本地方向调用它,它会返回相应的世界方向,你可以给CharacterController.Move

    void Update()
    {
        if (characterController.isGrounded)
        {
            // We are grounded, so recalculate
            // move direction directly from axes
    
            moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
            moveDirection = transform.TransformDirection(moveDirection);
            moveDirection = moveDirection * speed;
    
            if (Input.GetButton("Jump"))
            {
                moveDirection.y = jumpSpeed;
            }
        }
    
        // Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
        // when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
        // as an acceleration (ms^-2)
        moveDirection.y -= gravity * Time.deltaTime;
    
    
        // Move the controller
        characterController.Move(worldMoveDirection * Time.deltaTime);
    }
    

    事实上,CharacterController.Move 的旧文档就是使用这种方法!

    void Update()
    {
        if (controller.isGrounded)
        {
            // We are grounded, so recalculate
            // move direction directly from axes
    
            moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
            moveDirection = transform.TransformDirection(moveDirection);
            moveDirection = moveDirection * speed;
    
            if (Input.GetButton("Jump"))
            {
                moveDirection.y = jumpSpeed;
            }
        }
    
        // Apply gravity
        moveDirection.y = moveDirection.y - (gravity * Time.deltaTime);
    
        // Move the controller
        controller.Move(moveDirection * Time.deltaTime);
    }
    

    【讨论】:

      【解决方案2】:

      我是因为Move 使用世界空间坐标,但您生成的是本地坐标。

      尝试改变

        moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
      

        moveDirection = transform.right * Input.GetAxis("Horizontal") + transform.forward * Input.GetAxis("Vertical");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-25
        • 1970-01-01
        相关资源
        最近更新 更多