MiniGame1: GrandPrix Monaco
You will:
Read the doc
Manage Input
Control a car by translation/rotation
Create obstacles
Create a chase camera
End the race
Unity doc
How can we find information?
Rotate function
For example, if we want to learn more about the Rotate function, we can search in different ways:
Vector3
Find the documentation about the Vector3 class. You must find this page:
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
Input.GetAxis()
To simplify the code, Unity combines keyboard controls and joystick controls through the Input object. Thus, by using the member function GetAxis(« Horizontal »), we request information about the horizontal control: left/right arrow keys and left/right joystick.
Create a new script for the car
Paste the following code
using UnityEngine; public class carcontrol : MonoBehaviour { void Start() { } void Update() { float v = Input.GetAxis("Horizontal"); Debug.Log(v); } }
Run and check the values returned by the GetAxis function
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. If a rotation speed is set to 180°/s, it means that our object completes a full turn in 2 seconds or a half turn in 1 second.
We recall the formula that gives the rotation angle based on the rotation speed and the time interval:
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
![]()
Help in case nothing works
using UnityEngine; public class carcontrol : MonoBehaviour { public float rotSpeed; void Start() { } void Update() { float dT = Time.deltaTime; float hInput = Input.GetAxis("Horizontal"); transform.Rotate(Vector3.up, rotSpeed * dT * hInput); } }
Go forward
We want our vehicle to drive at a maximum speed of 10m/s.
Collision
Chase camera
We want the camera to follow the vehicle
![]()
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
![]()
Reset the camera transform node
Activate camera preview
Select translate tool
Move the camera to position it above the car’s roof with a downward view
![]()