- Home /
Understanding The Mouselook C# Script
I want to create a character control similar to that of Dungeon Defenders: movement left right and forward/backward is the w,a,s,d keys - but rotation/turning is done with the mouse. My first instinct was to look at the mouselook script which comes with Unity for any code I could salvage. I'm pretty much brand new to programming and so understanding this script is a real challenge - it doesn't help that there are no comments within the code's body.
Things that I don't understand:
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
Transform.LocalEulerAngles is described by the Unity API as: 'The rotation as Euler angles in degrees relative to the parent transform's rotation.' What is the parent transform's rotation? And why am I ADDING the mouse input along the x axis to this but focusing on the y axis with regards to the parent transform's y 'rotation'?
'rotationY += Input.GetAxis("Mouse Y") * sensitivityY;'
Why do I use +=? Don't get me wrong, I know what it means, rotationY = rotationY + Input.GetAxis("Mouse Y") etc. What's the point in setting it equal to itself here if it's an empty variable? Why do I need to plus two of the same variables together here?
I don't want to bog you guys down with everything I don't understand, those two appear to be the toughest concepts for me to grasp. As a matter of preference, please do not link me video tutorials. I'd like your own personal take on the issue if at all possible.
Thank you so much to anyone who can help.
Answer by robertbu · Oct 17, 2014 at 06:31 PM
A bit of house keeping. Naming is always a problem when using a mouse for rotation because typically 'x' movements of the mouse results in 'y' rotation of the object, and 'y' movement of the mouse results in 'x' rotation. And depending on the code, the naming may change. So you need to take a look at where the rotation is done to sort things out:
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
So you can see they are using 'Y' for the x component, and 'X' for the y component when building the Vector3. Now to your two lines:
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
So this line is taking the 'y' rotation of the object, and adding the Input from the x movement of the mouse to produce a new rotation. And this line.
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
Is adding or subtracting from 'rotationY' based on the 'y' movement of the mouse. Note that 'rotationY' is a class variable. While it starts out at 0.0, it keeps it value frame to frame. So it is not an empty variable...it only starts the game at 0.0.
'localEulerAngles' is the rotation of an object with respect to the parent. Typically this code would be placed on camera without a parent, so it would be the same as world rotation. But imagine this code was used to rotate a turret/gun on a tank. You would want the turret/gun rotation to be relative to the tank body.
One way to understand this code is to do a Debug.Log() of RotationY and RotationX, and place the code on another object (not the camera). Also it is a bit dangerous to read from eulerAngles as this code does here. It will work given strict limits to the ways something can rotate, but it will fail for arbitrary rotations. eulerAngles is derived from the the transform.rotation Quaternion, and will change representation. For example (180,0,0) is the same as (0,180,180). So you cannot use code similar to MouseLook for arbitrary rotations.
Your answer
