Lesson 1.4 - Step into the Driver's Seat
50 Mins
For Educators
In this lesson, we need to hit the road and gain control of the vehicle. In order to do so, we need to detect when the player is pressing the arrow keys, then accelerate and turn the vehicle based on that input. Using new methods, Vectors, and variables, you will allow the vehicle to move forwards or backwards and turn left to right.
Project Outcome:
When the player presses the up/down arrows, the vehicle will move forward and backward. When the player presses the left/right arrows, the vehicle will turn.
Recommended Unity Versions
2018.4 and Above
Lesson 1.4 - Step into the Driver's Seat

Allow the vehicle to move left/right
Until now, the vehicle has only been able to move straight forward along the road. We need it to be able to move left and right to avoid the obstacles.

  1. At the top of PlayerController.cs, add a public float turnSpeed; variable
  2. In Update(), add transform.Translate(Vector3.right * Time.deltaTime * turnSpeed);
  3. Run your game and use the turnSpeed variable slider to move the vehicle left and right

Base left/right movement on input
Currently, we can only control the vehicle’s left and right movement in the inspector. We need to grant some power to the player and allow them to control that movement for themselves.
  1. In PlayerController.cs, add a new public float horizontalInput variable
  2. In Update, assign horizontalInput = Input.GetAxis("Horizontal");, then test to see it in inspector
  3. Add the horizontalInput variable to your left/right Translate method to gain control of the vehicle
  4. In the inspector, edit the turnSpeed and speed variables to tweak the feel

Take control of the vehicle speed
We’ve allowed the player to control the steering wheel, but we also want them to control the gas pedal and brake

  1. Declare a new public forwardInput variable
  2. In Update, assign forwardInput = Input.GetAxis("Vertical");
  3. Add the forwardInput variable to the forward Translate method, then test

Make vehicle rotate instead of slide
There’s something weird about the vehicle’s movement… it’s slides left to right instead of turning. Let’s allow the vehicle to turn like a real car!

  1. In Update, call transform.Rotate(Vector3.up, horizontalInput), then test
  2. Delete the line of code that translates Right, then test
  3. Add * turnSpeed * Time.deltaTime, then test

Clean your code and hierarchy
We added lots of new stuff in this lesson. Before moving on and to be more professional, we need to clean our scripts and hierarchy to make them more organized.

  1. In the hierarchy, Right-click > Create Empty and rename it “Obstacles”, then drag all the obstacles into it
  2. Initialize variables with values in PlayerController, then make all variables private (except for the player variables)
  3. Use // to add comments to each section of code

Lesson Recap

New Functionality
  • When the player presses the up/down arrows, the vehicle will move forward and backward
  • When the player presses the left/right arrows, the vehicle turns
New Concepts & Skills
  • Empty objects
  • Get user input
  • Translate vs Rotate