Unity 3D Slides - Obstacle Course
Unity 3D Slides - Obstacle Course
Rick Davidson
Unity 2020.1
Rick Davidson
Core Gameplay Overview
Ramps for
“Bump” tally Obstacles
momentum
(score) to avoid
Camera (follows
player)
Falling
things
Spinny
things
Player
(moveable)
Game Design
Player Experience:
Careful? Clever? Nah
Nimble / agile
Core Mechanic:
Move & dodge obstacles
Game Loop:
Get from A to B
A Quick, Silly Challenge
● Give your Player a name.
● Mine is going to be “Dodgy”.
Unity 2020.1
Rick Davidson
Get The Party Started
● Create a new Unity 3D project
● Add a ground plane
● Create your “player”
● Rename your player
Unity 2020.1
Introducing Variables
Rick Davidson
Variables Are Like Boxes
● Variables help us store, manipulate and refer to
information
● Each variable has a NAME
20
● Each variable contains DATA
● Each variable is of a particular TYPE
hitPoints
int hitPoints = 20;
Variables Are Like Boxes
3.8
float speed = 3.8f;
true speed
bool isAlive = true;
isAlive Rick
Using SerializeField
Rick Davidson
Serialize Your Other Variables
● Make all three of your variables serialized and
therefore accessible in the inspector.
● While in play mode, make your player run away
from the camera and then run back to the camera.
Unity 2020.1
Rick Davidson
Add Vertical Axis
● Update one of our variables so we are moving our
player forwards and backwards (along the ground
plane, not flying in the air).
Unity 2020.1
Time.deltaTime
Rick Davidson
Using Time.deltaTime
● Using Time.deltaTime Unity can tell us how long
each frame took to execute.
● When we multiply something by Time.deltaTime
it makes our game “frame rate independent”.
● Ie. The game behaves the same on fast and slow
computers
On Update (each frame) move 1 unit to the left
Slow Fast
Computer Computer
Rick Davidson
What Is Cinemachine?
● Cinemachine is a powerful package that lets us:
○ manage multiple cameras in our scene
○ Easily create rules for our cameras
Main Camera
Cinemachine
Brain 2
Virtual
1 2 3
Cameras
Add A Follow Camera
● Install Cinemachine from Package Manager
● Add Cinemachine Brain component to camera
● Add Virtual Camera
● Rename Virtual Camera
● Point the Virtual Camera to follow the player
● Tune distance
Unity 2020.1
Basic Collisions
Rick Davidson
Make Some Walls
● Add some walls around the outside of your play
area
● If you like, make them a different colour
Unity 2020.1
Introduction To Methods
Rick Davidson
What Are Methods?
● Methods (also called Functions) execute blocks of
code that makes our game do things.
● To achieve this we must:
1. DECLARE and define our method
2. CALL our method when we want it to execute
Real World Example
DECLARE Method CALL Method
CleanYourRoom
- Pick up clothes Hey, go and
- Throw out garbage CleanYourRoom
- Kill the mutated science
project that threatens to
destroy life as we know it “Go and do the steps we’ve
defined and agreed to”
Syntax Used For Declaring Method
void CleanYourRoom()
{
Things To Do;
}
Return value Function name Parameter
void = return nothing WHAT to do () = nothing required
More Flavour Can Be Added
Two more common things when calling methods:
1. We can ask for some information to be RETURNED
2. We can specify some PARAMETERS are needed
when calling the method
void CleanYourRoom()
{
Things To Do;
}
Return value Function name Parameter
void = return nothing WHAT to do () = nothing required
Syntax Used For Declaring Method
bool CleanYourRoom(int time)
{
Things To Do;
Deadline = time;
return isRoomDirty;
} Parameter
Return value Return keyword Needs type
(Type = bool) (Type = bool) and name
Syntax Used For Calling Method
CleanYourRoom(); Remember
semi colon
Rick Davidson
Call Our Method
● Call our method so that our instructions print out
one time to the console.
Unity 2020.1
Using OnCollisionEnter()
Rick Davidson
Get Set Up
● Create a new C# script called ObjectHit
● Attach that script to all 4 walls
Unity 2020.1
Using GetComponent<>()
Rick Davidson
Unity 2020.1
Incrementing A Score
Rick Davidson
Prepare For Score Feedback
● Create a new c# script called Scorer
● Create a new OnCollisionEnter() method
● When we hit something, print to the console,
“You’ve bumped into a thing this many times: ”
Unity 2020.1
Using Time.time
Rick Davidson
Problem & Solution
Problem to solve:
● Make an object fall after 3 seconds has passed
Solution:
1. A timer - Time.time
2. A mechanism to “do a thing if 3 seconds has elapsed” - if
statement
3. A way to start the object falling after 3 seconds - disable /
enable gravity
Print Out Time Elapsed
● On every frame, print out to the console how much
time has elapsed since the game started.
● HINT: Use Time.time within debug.log
● Add your script to a game object
Unity 2020.1
If Statements
Rick Davidson
Conditional if Statement
if (some expression evaluates to true)
{
// execute this code block only
}
In Other Words...
if (in the mood for Pwning Noobs)
{
// play some PlayStation
true
} Play
Play?
PlayStation
false
Take No
Action
Use A Variable For Time To Wait
● Instead of “hard coding” 3 seconds, use a variable
for the time to wait that can be easily changed in
the Inspector.
Unity 2020.1
Caching A Reference
Rick Davidson
Simple Definition...
Caching is a technique of storing frequently used
data/information in memory, so that it can easily be
accessed when needed.
Make It Fall!
● Add 2 lines to our code that make our object
become visible and fall after it has waited for the
right amount of time.
Unity 2020.1
Using Tags
Rick Davidson
Unity 2020.1
Rotate An Object
Rick Davidson
Create Variables For X, Y & Z
● Serialize 3 variables and pass those in to our
Rotate method so that our object spins.
Unity 2020.1
Rick Davidson
Create Prefabs
● Create Prefabs for the following objects:
○ Dropper
○ Spinner
○ Roller
○ Obstacle
Unity 2020.1
Rick Davidson
Build Your Obstacle Course!
● Create a fun layout that makes the player go from A
to B.
● Use your droppers, rollers and spinners to create
interesting moments for the player.
Unity 2020.1
Rick Davidson