Unity Beginner to Hero Part 6: Vertical Rotation

Last time we modified our controller to move in the direction he was pointing, with his rotation being controller by the mouse’s x movement. This time, we’ll add in in a new axis of rotation to allow the controller to look up and down.

The first thing we need to do is give our controller a “head” to look around. Right-click the Character in the Hierarchy and select Create Empty. This will add a new, empty Game Object as a child of our Character. Rename the new object “Head” and set it’s position to 0, 0.5, 0. This will place it roughly where a head should be on the controller. Finally, drag the Nose object onto the Head in the Hierarchy to make it a child of Head. This way, the Nose will follow the Head’s position and rotation, again helping us know which direction he is pointing.

New Hierarchy, with the head added.

New Hierarchy, with the head added.

Open up your movement script and let’s add some neck rotation in. Our goal will be to rotate the Head’s Transform based on the motion of the mouse along the y axis. To do this, we’ll first need to be able to access the Head’s Transform in our script. Add the following line.

public Transform head;

Place it at the top alongside the other public variables. Unlike the other public variables we’ve defined, we did not set an initial value for this one; we’ll do that later in the Inspector. For now we can assume that this variable will eventually point to the Head object.

Our mouse input line we added in the previous lesson already grabs the input along the Y axis, leaving our only remaining task being to actually rotate the head. We’ll do this in a similar fashion to how we rotated our controller.

head.Rotate(Vector3.left, mouseInput.y * rotationSpeed);

Save your script and return to the editor. You’ll see that your new Transform head variable has appeared in the script, currently set to None (Transform). This tells us that the variable does not have any value at the moment, and if we want to set one it will need to be of the type Transform. Without deselecting the controller, drag the Head object from the Hierarchy into the slot. Doing this will automatically retrieve the Transform component from the Head and assign it to the new variable. (Alternatively, you can click the small circle beside the None (Transform) entry and select the Head object from the list.)

Play the game, and you’ll be able to make the controller look up and down!

Lesson 6 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 )

Connecting to %s