[英]How do I smoothly clamp controller input?
I'm trying to rotate an object according to the stick input on a controller, and then clamp it at a maximum rotation.我正在尝试根据 controller 上的摇杆输入旋转 object,然后将其夹在最大旋转处。 When I use Mathf.Clamp()
the rotation just hits that wall, and it doesn't feel good.当我使用Mathf.Clamp()
时,旋转会碰到那堵墙,感觉不太好。 I've tried using Mathf.SmoothDamp()
as well and that creates similar results.我也尝试过使用Mathf.SmoothDamp()
并产生类似的结果。 Is there a way to smoothly approach the max rotation angle while taking the stick input into account?有没有办法在考虑摇杆输入的同时平滑地接近最大旋转角度?
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);
}
Any help is greatly appreciated.任何帮助是极大的赞赏。
Try this Stuff试试这个东西
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.