我想夾緊旋轉,旋轉由WASD鍵控制。
我怎樣才能做到這一點?我嘗試了很多東西,但它不起作用。
float vertical = Input.GetAxis("Vertical");
float horizontal = Input.GetAxis("Horizontal");
transform.Rotate(vertical / 4, horizontal / 4, 0.0f);
uj5u.com熱心網友回復:
您要做的是存盤絕對值,例如
// 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);
}
uj5u.com熱心網友回復:
使用 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://www.uj5u.com/qiye/365043.html
