[英]Unity Clamping Rotation that is controlled by WASD
I want to clamp the rotation, the rotation is controlled by the WASD keys.我想夹住旋转,旋转由WASD键控制。
How can i do this?我怎样才能做到这一点? I tried many things but it just doesn't work.
我尝试了很多东西,但它不起作用。
float vertical = Input.GetAxis("Vertical");
float horizontal = Input.GetAxis("Horizontal");
transform.Rotate(vertical / 4, horizontal / 4, 0.0f);
What you will want to do is store the absolute values like eg您要做的是存储绝对值,例如
// Adjust these via Inspector
public float minPitch = -45;
public float maxPitch = 45;
public float minYaw = -90;
public float maxYaw = 90;
public Vector2 sensitivity = Vector2.one * 0.25f;
private float pitch;
private float yaw;
private Quaternion originalRotation;
private void Awake ()
{
originalRotation = transform.rotation;
}
void Update ()
{
pitch += Input.GetAxis("Vertical") * sensitivity.x;
yaw += Input.GetAxis("Horizontal") * sensitivity.y;
pitch = Mathf.Clamp(pitch, minPitch, maxPitch);
yaw = Mathf.Clamp(yaw, minYaw, maxYaw);
transform.rotation = originalRotation * Quaternion.Euler(pitch, yaw, 0);
}
Use Math.Clamp()使用 Math.Clamp()
float vertical = Input.GetAxis("Vertical");
float horizontal = Input.GetAxis("Horizontal");
float minClamp = -5.0f;
float maxClamp = 5.0f;
transform.Rotate(Math.Clamp(vertical / 4, minClamp, maxClamp),
Math.Clamp(horizontal / 4, minClamp, maxClamp));
https://docs.microsoft.com/en-us/dotnet/api/system.math.clamp?view=net-6.0#System_Math_Clamp_System_Single_System_Single_System_Single _ https://docs.microsoft.com/en-us/dotnet/api/system.math.clamp?view=net-6.0#System_Math_Clamp_System_Single_System_Single_System_Single _
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.