Obstacle Course
We will create an obstacle course similar to the one in the screenshot above with the parts functioning as follows:
We will create an obstacle course similar to the one in the screenshot above with the parts functioning as follows:
Start:
Start:
Add a Script called Mover and attach it to your Player.
Add a Script called Mover and attach it to your Player.
Create materials for the Plane and the Player
Create materials for the Plane and the Player
Create 3 float variables for your player (xValue, yValue and zValue) and Serialize them.
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.
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
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.
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.
This will allow you to fine tune the movement of the player.
Cinemachine
Cinemachine
Scene Construction
Scene Construction
Add four walls around the Plane so that the player cannot escape the course.
Add four walls around the Plane so that the player cannot escape the course.
Create a new material for your walls
Create a new material for your walls
Add a Rigidbody component to the Player
Add a Rigidbody component to the Player
Freeze the Y position
Freeze the Y position
Freeze the X, Y and Z Rotation
Freeze the X, Y and Z Rotation
Methods
Methods
Create a MovePlayer Method and call it in the Update method.
Create a MovePlayer Method and call it in the Update method.
Collisions
Collisions
See if you can adapt the method above to change the colour of the wall you hit.
See if you can adapt the method above to change the colour of the wall you hit.
Implementing a score
Implementing a score
Attach the Scorer to the Player
Attach the Scorer to the Player
Introducing falling obstacles
Introducing falling obstacles
Make a new C# Script and call it Dropper
Make a new C# Script and call it Dropper
Add a dropping obstacle to the course and attach the Dropper Script to it.
Add a dropping obstacle to the course and attach the Dropper Script to it.
Our plan now is to:
Our plan now is to:
- Hide the floating obstacles until they are ready to drop by disabling the MeshRenderer.
- Disable gravity on the Rigidbody until the objects are ready to drop then enable it.
We need a reference to the MeshRenderer and the Rididbody
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.
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:
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.
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:
Now we need to adjust our program:
- to ensure that the score doesn't increase when the player bumps the same object more than once.
- to ensure that the objects only turn red when they collide with the player.
Add the built-in Player tag to your Player Game Object.
Add the built-in Player tag to your Player Game Object.
This will allow us to identify when the Player has collided with something.
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".
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:
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
Now modify the code in ObjectHit
Don't forget to modify the code in the Scorer class as well!
Don't forget to modify the code in the Scorer class as well!
Rotating objects
Rotating objects
Add a new Script to the game and call it Spinner
Add a new Script to the game and call it Spinner
This will control the rotation of our spinning objets.
This will control the rotation of our spinning objets.
Add the Spinner script and the ObjectHit script to any obstacles which will rotate.
Add the Spinner script and the ObjectHit script to any obstacles which will rotate.