- Home /
Explaining A Mouse Look Script
I recently found a Mouse Look Script online that works perfectly fine. However, rather than just copying and pasting, then dragging and dropping, I was wondering if there was anyone who could explain this script line for line. Much thanks in advance!
Here's the script: @script AddComponentMenu ("Camera-Control/Mouse Look") enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 } var axes = RotationAxes.MouseXAndY; var sensitivityX : float = 15; var sensitivityY : float = 15;
var minimumX : float = -360; var maximumX : float = 360;
var minimumY : float = -60; var maximumY : float = 60;
var rotationX : float = 0; var rotationY : float = 0;
private var originalRotation : Quaternion;
function Update () { if (axes == RotationAxes.MouseXAndY) { rotationX += Input.GetAxis("Mouse X") sensitivityX; rotationY += Input.GetAxis("Mouse Y") sensitivityY;
rotationX = ClampAngle (rotationX, minimumX, maximumX);
rotationY = ClampAngle (rotationY, minimumY, maximumY);
var xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
var yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
transform.localRotation = originalRotation * xQuaternion * yQuaternion;
}
else if (axes == RotationAxes.MouseX) {
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationX = ClampAngle (rotationX, minimumX, maximumX);
xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
transform.localRotation = originalRotation * xQuaternion;
}
else {
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = ClampAngle (rotationY, minimumY, maximumY);
yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
transform.localRotation = originalRotation * yQuaternion;
}
}
function Start () { if (rigidbody) rigidbody.freezeRotation = true; originalRotation = transform.localRotation; }
static function ClampAngle (angle : float, min : float, max : float) : float { if (angle < -360.0) angle += 360.0; if (angle > 360.0) angle -= 360.0; return Mathf.Clamp (angle, min, max);
}
Answer by Berenger · Feb 21, 2012 at 10:16 PM
I don't know about line by line, but here is the idea :
Get the mouse input.
Clamp the angle between a min and a max. ( Basically, when you look up, it blocks from going upside down )
Creating the rotation made by that input.
Affecting the rotation the object.
By "mouse input" do you mean the location of the mouse?