Unity Beginner to Hero Part 4: Rotation

Alright ladles and gentlespoons, welcome back for part four. Our goal for this lesson is to use mouse input to rotate our character. Before we do this, we should first notice that our character is currently circularly symmetrical; if we rotate it around, there’s currently no way to tell which way it’s pointing!

To fix this, create a new cube and name it “Nose.” Set it’s scale to be 0.5, 0.5, 0.5. Then, select the Nose in the Hierarchy panel and drag it onto the Character object. This will cause the Nose to be set as a child of the Character, meaning that when the Character moves, rotates or scales itself, the nose will move, rotate and scale with it. Once you’ve childed the Nose, set it’s position to 0, 0.5, 0.5. This way, it will point out the front of the Character and give us an indicator to what direction we’re pointing. Finally, remove the Box Collider object from the Nose to make sure our character doesn’t collide with it!


Onto scripting. Our first task is to retrieve mouse input, which we’ll do in a similar fashion to keyboard input. Add the following line at the top of the Update method.

Vector2 mouseInput = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));

Again we are storing our input axes in a Vector, but this time it is a Vector2, since the mouse cursor only moves in 2D space across the screen. We’ll also want to add a new variable to our class to control how fast our character rotates. Put this at the top with the speed variable.

public float rotationSpeed = 2.0f;

Finally, we need to actually rotate the character with our mouse input. Put the following line anywhere below the mouseInput declaration.

transform.Rotate(Vector3.up, mouseInput.x * rotationSpeed);

Once again we are manipulating the transform of our character. This time we are calling the Rotate method, which takes two parameters: a Vector3, and a float. This performs a rotation on our character using the Axis Angle method, where an axis is defined in space and then rotated about by a specified amount of degrees. In this case we are taking the up Vector (shorthand for Vector3(0, 1, 0)), and rotating about it using our mouse input. Unity follows what’s called the left-hand rule for rotations. To understand this, hold up your left hand with your thumb pointed outwards and your fingers curled around. Your thumb is the direction of the Vector, while your fingers denote the direction the object is rotated for positive rotations.

Positive rotations around an axis (represented by the thumb) follow the direction of your curled fingers.

Positive rotations around an axis (represented by the thumb) follow the direction of your curled fingers.

Run the game and you’ll be able to rotate the character in 360 degrees using your mouse. Unfortunately, you’ll notice that he doesn’t seem to walk in the direction he’s pointing! This is because we are still performing our movement in world-space, a problem we will fix in the next lession.

Lesson 4 Complete Project

Thanks for reading! If you have any problems with the above tutorial, general comments or suggestions, please post below. It’s always appreciated!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s