【问题标题】:Unity character controller preventing movement stacking with W + A/D keysUnity 角色控制器使用 W + A/D 键防止移动堆叠
【发布时间】:2020-07-30 15:09:25
【问题描述】:

我一直在尝试制作一个非常基本的 fps 角色控制器脚本,但我无法解决横向移动时的移动堆叠问题。我敢肯定它确实是基本的解决方案,但作为一个初学者,我很难解决它。

float forwardSpeed = Input.GetAxis("Vertical") * movementspeed;
float sideSpeed = Input.GetAxis("Horizontal") * movementspeed;

Vector3 VecForwardSpeed = new Vector3(sideSpeed, verticalVelocity, forwardSpeed);

VecForwardSpeed = transform.rotation * VecForwardSpeed;

characterController.Move(VecForwardSpeed * Time.deltaTime);

【问题讨论】:

    标签: unity3d controller character


    【解决方案1】:

    如果我理解正确,您的意思是,如果同时向前和侧向移动,这些输入/速度会“叠加”或相加,从而允许用户移动得比实际允许的更快。

    您可以通过规范化它们来解决这个问题,这意味着您确保它们的组合永远不会超过 1 的幅度值,例如

    // Get a vector of the combined input
    var combinedInput = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
    
    // Check if the magnitude exceeds 1 
    // sqrMagnitude is more efficient here and for comparing to 1
    // behaves the same as magnitude
    if(combinedInput.sqrMagnitude > 1)
    {
       // If so normalize the input vector to force it again
       // to have the maximum length/magnitude of 1
       combinedInput.Normalize();
    }
    // Until then apply the movementspeed here
    combinedInput *= movementspeed;
    
    // Now use the components of this combined and evtl normalized input vector instead
    var vecForwardSpeed = transform.rotation * new Vector3(combinedInput.x, verticalVelocity, combinedInput.y) * Time.deltaTime;
    characterController.Move(vecForwardSpeed);
    

    根据您的问题,不确定verticalVelocity 是如何发挥作用的。

    【讨论】:

    • 没错!也非常感谢您的解释!
    猜你喜欢
    • 1970-01-01
    • 2021-10-29
    • 2022-07-17
    • 2020-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-14
    相关资源
    最近更新 更多