首页 > 解决方案 > Unity3D - 第三人称玩家在对角线方向的移动比垂直和水平移动慢?

问题描述

我正在尝试创建一个角色控制器,该控制器允许移动速度受到玩家按下左摇杆的距离的影响。当我完成这么多工作时,我遇到的另一个问题是玩家在对角线移动时速度会降低。

我在网上找到的大多数信息都涉及对角线运动更快(运动矢量高于 1)的相反问题。解决方案是夹紧幅度,因此将摇杆压入角落不会超过 1。我尝试夹紧以查看是否可行,但是,我不相信这是解决我的问题的方法。

此处的代码允许玩家相对于相机移动,并且速度受输入向量的影响。

Vector3 input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
Vector3 inputDir = input.normalized;
void Move(Vector3 inputDir, Vector3 input)
    {
        running = Input.GetKey(KeyCode.Joystick1Button1); // B button on xbox one controller

        // Rotation stuff
        if (inputDir != Vector3.zero) {
            float targetRotation = Mathf.Atan2(inputDir.x, inputDir.z) * Mathf.Rad2Deg + cameraTransform.eulerAngles.y;
            transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, GetModifiedSmoothTime(speedSmoothTime));
        }

        // Pretty sure this is where things are breaking        
        float targetSpeed = ((running) ? runSpeed : walkSpeed) * inputDir.magnitude;
        currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, GetModifiedSmoothTime(speedSmoothTime));
        currentSpeed *= input.magnitude;
        vel = transform.forward * currentSpeed + Vector3.up * velocityY;
        currentSpeed = new Vector3(_controller.velocity.x, 0, _controller.velocity.z).magnitude;
    }
void ExecuteMovement()
    {            
        _controller.Move(vel * Time.deltaTime);
    }

标签: c#unity3d

解决方案


推荐阅读