MiniGame1: GrandPrix Monaco

You will:

  • How to find information

  • Manage Input

  • Control a car by translation/rotation

  • Snap vertex

  • Create a chase camera

  • Build a track

  • End the race using a finish line

Refresher

How to manage your view:

  • F key: center an object.

  • Right-click: look around.

  • ALT + Left-click: rotate camera while focusing on the selected object.

  • ALT + Right-click: zoom in & out.

  • Mouse wheel: zoom in & out.

  • Hold mouse wheel + mouse move: pan.

Car Controls

Scene Layout

  • In the Assets folder, create a new folder named MiniGames

  • Inside this folder, create a new Scene named: Ultimate Race

  • Using 3D Cubes:

    • Create a road of 50mx10m

    • Create a car 4mx2mx1m

    ../_images/scene2.png
  • Adjust your camera so the entire scene is visible in Play mode.

Input.GetAxis()

Unity combines both keyboard and joystick inputs through the Input utility class. By using the function GetAxis(« Horizontal »), we retrieve information about horizontal movement from either the arrow keys or/and the joystick.

  • Create a new script and attach it to the car

  • Set its name to CarControler

  • Paste the following code

using UnityEngine;

public class carControler : MonoBehaviour
{
        void Start() { }

        void Update()
        {
                float v = Input.GetAxis("Horizontal");
                Debug.Log(v);
        }
}
  • Enter Play mode

  • Use the left and right arrow keys and observe the values returned by the GetAxis function.

    ../_images/joy.png

Note

Unity seems to simulate joystick behavior through keyboard keys. For example, when we press the right arrow key, Unity smooths the return values from 0 to 1.

Physic’s law

In unity, rotations use degrees. We recall the formula that gives the rotation angle based on the rotation speed and the time interval:

\[angle = dT \times Rot_{speed}\]

Let’s turn

We want to make our vehicle rotate at a maximum speed of 30°/sec.

  • Add a public float variable turnSpeed in your script

  • In the update() function, add these lines:

    float dt =  Time.deltaTime;
    float hInput = Input.GetAxis("Horizontal");
    transform.Rotate( ... );
    
  • Check the documentation of the Rotate function

  • Using a simple operation, make it so that:

    • When input is 0, nothing happens

    • When input is 1, the vehicle turns at the speed turnSpeed

../_images/rot1.gif

Go forward

We want our vehicle to drive at a maximum speed of 30m/s.

  • Add a public float variable maxSpeed in your script

  • When the player press the up arrow key, the vehicle must go forward at the speed maxSpeed

../_images/go.gif

Buid a track

  • Select the piece of road in the scene

  • Duplicate it and move it away

  • In the Inspector, change its Y-value for 30°

  • Snap it to the end of the road

    ../_images/track.gif
  • Add multiple pieces to build a small track

    ../_images/fulltrack.png

Chase camera

We want the camera to follow the vehicle

../_images/finalcar.gif
  • In the Hierarchy, drag and drop the camera into the car GameObject

    • The camera becomes a child object of the car in the Hierarchy

    • This way, when the car moves, the camera automatically follows

    ../_images/child.png
  • Reset the camera transform node

    ../_images/reset1.png
  • Activate camera preview

    ../_images/campreview.png
  • Select translate tool

  • Move the camera to position it above the car’s roof with a downward view

    ../_images/preview.png

Finish line

  • Create a finish line

    ../_images/finish.png
  • Create a collider with a script to detect when the car passes between the two poles

    • Change the color of the banner from red to green

Add more fun

As you may have noticed, the car doesn’t fall when it goes off the track. To make things more fun and a bit more challenging, we’re going to simulate gravity. We’ll give a brief overview of the process here, as it will be covered in more detail in an upcoming chapter.

  • Select the Car GameObject

  • In the Inspector, click the Add Component button

  • Add a Rigidbody component

  • In the Rigidbody settings, make sure the Use Gravity option is enabled

  • Enter Play mode and have fun !

../_images/fun.gif