- Home /
Question by
GameMaker102 · Dec 12, 2016 at 08:07 AM ·
rotationscripting problemcamera-lookz-axis
Unity Mouse Look Script Issue
My mouse look script would work perfectly, if the camera wouldn't rotate on the Z axis. My script is:
public float xSensitivity = 10f;
public float ySensitivity = 10f;
public float minHeight = -360f;
public float maxHeight = 360f;
float rotationX = 0f;
float rotationY = 0f;
void Update() {
rotationX = Input.GetAxis ("Mouse X") * xSensitivity / 20;
rotationX = ClampAngle (rotationX, minHeight, maxHeight);
Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, transform.up);
transform.rotation = transform.localRotation * xQuaternion;
rotationY = Input.GetAxis ("Mouse Y") * ySensitivity / 20;
rotationY = ClampAngle (rotationY, minHeight, maxHeight);
Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, (transform.right * -1));
transform.rotation = transform.localRotation * yQuaternion;
Quaternion regRotation = transform.rotation;
transform.rotation = regRotation * xQuaternion * yQuaternion;
}
public static float ClampAngle(float angle, float min, float max) {
if( angle <= -360f) {
angle += 360f;
}
if (angle >= 360f) {
angle -= 360f;
}
return Mathf.Clamp (angle, min, max);
}
I don't know how to keep it from rotating on that axis. Any ideas on how to fix it?
Comment