Lesson 1.4 - Step into the Driver's Seat

Tutorial

·

Beginner

·

+10XP

·

50 mins

·

(4109)

Unity Technologies

Lesson 1.4 - Step into the Driver's Seat

Overview:

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.

Languages available:

Overview Video

1. 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

  1. In Update(), add transform.Translate(Vector3.right * Time.deltaTime * turnSpeed);

  1. Run your game and use the turnSpeed variable slider to move the vehicle left and right


2. 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. From the top menu, click Edit > Project Settings, select Input Manager in the left sidebar, then expand the Axes fold-out to explore the inputs

  1. In PlayerController.cs, add a new public float horizontalInput variable

  1. In Update, assign horizontalInput = Input.GetAxis("Horizontal");, then test to see it in inspector

  1. Add the horizontalInput variable to your left/right Translate method to gain control of the vehicle

  1. In the Inspector, edit the turnSpeed and speed variables to tweak the feel


3. 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

  1. In Update, assign forwardInput = Input.GetAxis("Vertical");

  1. Add the forwardInput variable to the forward Translate method, then test


4. Make vehicle rotate instead of slide

There’s something weird about the vehicle’s movement… it 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

  1. Delete the line of code that translates Right, then test

  1. Add * turnSpeed * Time.deltaTime, then test


5. 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

  1. Initialize variables with values in PlayerController, then make all variables private (except for the player variables)

  1. Use // to add comments to each section of code


6. 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

Complete this tutorial