【问题标题】:How do I smoothly clamp controller input?如何平滑地钳制控制器输入?
【发布时间】:2022-01-13 19:53:25
【问题描述】:

我正在尝试根据控制器上的摇杆输入旋转对象,然后将其夹在最大旋转位置。当我使用Mathf.Clamp() 时,旋转刚好碰到那面墙,感觉不太好。我也尝试过使用Mathf.SmoothDamp(),这会产生类似的结果。有没有办法在考虑摇杆输入的同时平滑地接近最大旋转角度?

    void Update()
    {
        SmoothRotate();
    }

    void SmoothRotate()
    {
        rotateX += iR.leftStickY * rotationSensitivity * Time.deltaTime;
        rotateX = Mathf.Clamp(rotateX, -maxPitchAngle, maxPitchAngle);
        currentRotX = Mathf.Lerp(currentRotX, rotateX, .5f);

        rotateZ += iR.leftStickX * rotationSensitivity * Time.deltaTime;
        rotateZ = Mathf.Clamp(rotateZ, -maxPitchAngle, maxPitchAngle);
        currentRotZ = Mathf.Lerp(currentRotZ, rotateZ, .5f);

        transform.eulerAngles = new Vector3(currentRotX, currentAngle.y, currentRotZ);
    }

非常感谢任何帮助。

【问题讨论】:

    标签: c# unity3d smoothing lerp clamp


    【解决方案1】:

    试试这个东西

     public float mouseSensitivity = 10.0f;
     public Transform target;
     public float dstFromTarget = 2.0f;
     public float yaw;
     public float pitch;
     public Vector2 pitchMinMax = new Vector2(-50, 85);
     public float rotationSmoothTime = 0.02f;
     Vector3 rotationSmoothVelocity;
     Vector3 currentRotation;
    
    
     void Update()
     {
         SmoothRotate();
     }
    
     void SmoothRotate()
     {
         //Mouse Movement
         yaw += iR.leftStickX * mouseSensitivity;
         pitch -=  iR.leftStickY * mouseSensitivity;
     
         // Clamp
         pitch = Mathf.Clamp(pitch, -maxPitchAngle, maxPitchAngle);
    
      
         transform.eulerAngles = Vector3.SmoothDamp(currentRotation, new Vector3(pitch, yaw), ref rotationSmoothVelocity, rotationSmoothTime);
       
    
     }
    

    【讨论】:

    • 因此,当向前投球时,这在 x 轴上效果很好,但在向后投球时,事情开始不规律地旋转。 z 轴也没有被夹住,可以随心所欲地旋转。我将偏航改为滚动,因为旋转是围绕 z 轴并添加roll = Mathf.Clamp(roll, -maxPitchAngle, maxPitchAngle); 来夹紧它。
    猜你喜欢
    • 1970-01-01
    • 2012-03-22
    • 1970-01-01
    • 2010-11-23
    • 2017-07-02
    • 1970-01-01
    • 1970-01-01
    • 2022-09-24
    • 1970-01-01
    相关资源
    最近更新 更多