Obstacle Course

We will create an obstacle course similar to the one in the screenshot above with the parts functioning as follows:

Start:

Add a Script called Mover and attach it to your Player.

Create materials for the Plane and the Player

Create 3 float variables for your player (xValue, yValue and zValue) and Serialize them.

Add the above code to your Update method and test your player movement in game.

Multiply your x and y values by Time.deltaTime to ensure our player movement is frame rate independent

Add another variable called moveSpeed and multiply your x and y values by it.

This will allow you to fine tune the movement of the player.

Cinemachine

Scene Construction

Add four walls around the Plane so that the player cannot escape the course.

Create a new material for your walls

Add a Rigidbody component to the Player

Freeze the Y position

Freeze the X, Y and Z Rotation

Methods

Create a MovePlayer Method and call it in the Update method.

Collisions

See if you can adapt the method above to change the colour of the wall you hit.

Implementing a score

Attach the Scorer to the Player

Introducing falling obstacles

Make a new C# Script and call it Dropper

Add a dropping obstacle to the course and attach the Dropper Script to it.

Notice HOW Time.time works.


This is a good point to read the Unity Documentation.


Unity API 

Our plan now is to:

We need a reference to the MeshRenderer and the Rididbody

It's good to also have a variable to adjust the amount of time before the obstacles fall.

Now that we have a reference to these components we can manipulate their properties:

In the Update method we can now make the object visible after 3 seconds and enable the gravity.

Now we need to adjust our program:

Add the built-in Player tag to your Player Game Object.

This will allow us to identify when the Player has collided with something.


Now try adding an IF-Statement to your ObjectHit Script to only turn red when colliding with something tagged as "Player".

We should also tag everything we have hit so that we only increase the score when we collide with something new:

Now modify the code in ObjectHit

Don't forget to modify the code in the Scorer class as well!

Rotating objects

Add a new Script to the game and call it Spinner

This will control the rotation of our spinning objets.

Add the Spinner script and the ObjectHit script to any obstacles which will rotate.