Project 1A: Barrel Bouncer (Setup)

Part 4: Allowing Users to Move Around

Whew! You now have your environment laid out. However, if you click the play button to enter Play Mode and try out the game in the Game View, you will notice that you just stay in place and nothing happens, no matter what you do! What good is your beautiful environment if you can’t even move around to see it all?

If you exit Play Mode, go back into the Scene View, and hold down the right-click mouse button, you will see that you can easily navigate around the scene using the WASD keys and can use your mouse movements to change the direction you’re facing. In this section, you will try to imitate this functionality for use in Play Mode. It may be worth looking at the project example once again to get a feel for what you are expected to complete.

Your work in this section will serve as the basis for the player controller, something you will build upon in projects 1B & 1C. A player controller (also known as a character controller) is a component that serves as a link between input from the human user and the main “player” GameObject that the script controls. Its core functionality is to control the player GameObject’s movement, but there are many other actions it may define as well. Some common functionalities that a character controller could provide include the ability to…

A player controller can define functionality from either a first-person perspective (such as in games like Minecraft or Call of Duty) or a third-person perspective (such as in games like Fortnite, Mario Kart, or League of Legends).

Note that while there are many character controllers available on the asset store and via other sources (including a built-in Unity component), you are required to implement your own from scratch for this project.

Setting Up the Player

Your player will consist of two GameObjects: one for the body and one for the head, with the head parented to the body. This allows both GameObjects to move in different ways, something that would not be possible with a single GameObject (and thus just a single Transform component). This is important because it ensures that the rotations of the head do not affect the body. For example, if the head looks up and down, the body does not bend or slant forwards and backwards as well, but instead still stays upright. At the same time, the head’s position is still relative to the body’s, and thus it still moves correctly when the whole body moves.

Complete the following steps to set up the player:

  1. On the menu bar, click on GameObject -> 3D Object -> Capsule. This will create a pill shaped object that you will use as a representation of your character’s physical body.
  2. Set its position to the origin (0, 0, 0) if it didn’t already instantiate at that location.
  3. Rename the Capsule GameObject to “Player”.
  4. Set the Tag of the Player GameObject to Player.
  5. Make the Main Camera GameObject a child of this new Player GameObject by clicking and dragging the Main Camera GameObject onto the Player GameObject in the hierarchy.
  6. Set the Main Camera GameObject’s local position to the point (0, 1, 0). This will place it one meter above the center of the Player GameObject, which is at the top of the capsule.
  7. Rename the Main Camera GameObject to “Head”.
  8. Move the Environment GameObject’s position so that its center is roughly at the position of the Player GameObject and its base/ground is either tangentially touching or almost touching the bottom of the capsule (i.e. the feet of the player). This will ensure that the user is right at the center of the environment when they start the game.
  9. Select the Player GameObject and then attach a new script called “PlayerController” to it via the Add Component button in the Inspector.

GIF showing setup of Player GameObject

You may be wondering why we moved the entire environment, rather than just move the player’s position onto an appropriate location in the environment. While it may not make much of a difference now, some XR SDKs rely on the fact that the player starts at position (0, 0, 0) and that the headset has complete control over the player’s position and rotation, so it is a good practice to do that now as well.

Creating the Script

You can now double click on the new script that appears in your project window to open it up in your C# IDE and begin coding!

Requirements

The basic requirements for your character controller in this section are:

The GIF below shows the gameplay of a completed project 1A, as well as the effects of the various movements on the Player’s and Head’s transforms and the Camera in the scene view.

GIF showing movement effect on transform in scene view

Component Properties (Fields)

Your class should have three fields: two floats to control the speeds of the translational and rotational movements of the player and one reference to the Transform component of the Head GameObject.

The reason you need a reference to the Head’s Transform is that the Head is the GameObject that contains the Main Camera in the scene that needs to be controlled by the Player Controller and the Player Controller does not have a way to access the Head’s Tranform by default, since it is on the Player GameObject, a completely different GameObject.

Additionally, you should set the default values of the translational and rotational speeds to 1. This will allow you to easily test the default speeds of your movement so you can then decide whether to scale them faster or slower in the Editor.

All of these fields should be public so that you are able access and modify them in the Editor. Below is the code that you should have to define your fields.

public float translationalSpeed = 1.0f, rotationalSpeed = 1.0f;
public Transform cameraTransform;

The image below shows some possible modifications you could make to the fields of this component instance within the Inspector:

Image that shows PlayerController script properties in the Inspector

Important Notes on Your Implementation

In order to use the input system to get input from the mouse and keyboard, you should use method(s) from the Input class (documentation linked here) in your code. It is highly suggested that you read through the description in its documentation to figure out how you will get the appropriate input and what you will need to do before starting to code.

In addition to the Input class, the only Unity classes you are only allowed to use and reference in your implementation for this part of the project are those that are directly related to the items we have discussed in class. Specifically, you may refer to methods and properties of the GameObject, Transform, Vector3, MonoBehaviour, MeshFilter, MeshRenderer, and Material classes, though you certainly do not need to refer to all of them. However, you may NOT use references, properties, or methods of other Unity classes in your implementation, such as that of the Rigidbody or the built-in Character Controller. If you do so, we will assume that you must have copied the code from some other source, and at a minimum, you will not receive points for this section.

Previous Section | Go Home | Next Section