Lesson Plan 3.1 - Jump Force
Lesson Plan 3.1 - Jump Force
Step 2: Choose and set up a player character Example of project by end of lesson
Step 3: Make player jump at start
Overview: The goal of this lesson is to set up the basic gameplay for this prototype. We
will start by creating a new project and importing the starter files. Next we
will choose a beautiful background and a character for the player to control,
and allow that character to jump with a tap of the spacebar. We will also
choose an obstacle for the player, and create a spawn manager that throws
them in the player’s path at timed intervals.
Project The character, background, and obstacle of your choice will be set up. The
Outcome: player will be able to press spacebar and make the character jump, as
obstacles spawn at the edge of the screen and block the player’s path.
Step 2: Choose and set up a player character
Now that we’ve started the project and chosen a background, we need to set up a character for
the player to control.
1. From Course Library > Characters, Drag a - Don’t worry: We will get the player and
character into the hierarchy, rename i t “Player”, the background moving soon
then rotate it on the Y axis to face to the right - Warning: Keep isTrigger UNCHECKED!
- Tip: Use isometric view and the
2. Add a Rigid Body component
gizmos to cycle around and edit the
3. Add a box collider, then edit the collider bounds collider with a clear perspective
4. Create a new “Scripts” folder in Assets, create a
“PlayerController” script inside, and attach it to the
player
void Start()
{
playerRb = GetComponent<Rigidbody>();
playerRb.AddForce(Vector3.up * 1000);
}
Step 4: Make player jump if spacebar pressed
We don’t want the player jumping at start - they should only jump when we tell it to by pressing
spacebar.
1. In Update() add an i f-then statement checking if - Warning: Don’t worry about the slow
the spacebar is pressed jump double jump, or lack of
2. Cut and paste the AddForce code from Start() into animation, we will fix that later
- Tip: Look at Unity documentation for
the if-statement
method overloads here
3. Add the ForceMode.Impulse parameter to the - New Function: ForceMode.Impulse
AddForce call, then r educe force multiplier value and optional parameters
void Start()
{
playerRb = GetComponent<Rigidbody>();
playerRb.AddForce(Vector3.up * 100);
}
void U pdate() {
if (Input.GetKeyDown(KeyCode.Space)) {
ForceMode.Impulse); } }
playerRb.AddForce(Vector3.up * 100,
void Start() {
playerRb = GetComponent<Rigidbody>();
Physics.gravity *= gravityModifier; }
void U pdate() {
if (Input.GetKeyDown(KeyCode.Space)) {
playerRb.AddForce(Vector3.up * 10 jumpForce, ForceMode.Impulse); } }
Step 6: Prevent player from double-jumping
The player can spam the spacebar and send the character hurtling into the sky. In order to stop
this, we need an if-statement that makes sure the player is grounded before they jump.
1. Add a new public bool isOnGround variable and - New Concept: Booleans
set it equal to true - New Concept: “And” operator (&&)
2. In the if-statement making the player jump, set - New Function: OnCollisionEnter
- Tip: When assigning values, use one =
isOnGround = false, then test
equal sign. When comparing values,
3. Add a condition && isOnGround to the use == two equal signs
if-statement
4. Add a new void onCollisionEnter method, set
isOnGround = true in that method, then test
void U pdate() {
if (Input.GetKeyDown(KeyCode.Space) && isOnGround) {
playerRb.AddForce(Vector3.up * 10 jumpForce, ForceMode.Impulse);
isOnGround = false; } }
void Update() {
transform.Translate(Vector3.left * Time.deltaTime * speed);
}
Step 8: Create a spawn manager
Similar to the last project, we need to create an empty object Spawn Manager that will
instantiate obstacle prefabs.
1. Create a new “Spawn Manager” empty object, then - Don’t worry: We’re just instantiating on
apply a new SpawnManager.cs script to it Start for now, we will have them
2. In SpawnManager.cs, declare a new public repeating later
- Tip: You’ve done this before! Feel free
GameObject obstaclePrefab;, then assign your
to reference code from the last project
prefab to the new variable in the inspector
3. Declare a new private Vector3 spawnPos at your
spawn location
4. In Start(), Instantiate a new obstacle prefab, then
delete your prefab from the scene and test
void Start() {
Instantiate(obstaclePrefab, spawnPos, obstaclePrefab.transform.rotation); }
© Unity 2019 Lesson 3.1 - Jump Force
6
Step 9: Spawn obstacles at intervals
Our spawn manager instantiates prefabs on start, but we must write a new function and utilize
InvokeRepeating if it to spawn obstacles on a timer. Lastly, we must modify the character’s
RigidBody so it can’t be knocked over.
1. Create a new void SpawnObstacle method, then - New Concept: RigidBody constraints
move the Instantiate call inside it
2. Create new float variables for s tartDelay and
repeatRate
3. Have your obstacles spawn on i ntervals using the
InvokeRepeating() method
4. In the Player RigidBody component, expand
Constraints, then Freeze all but the Y position
loat startDelay = 2;
private f
private f loat repeatRate = 2;
void Start() {
InvokeRepeating("SpawnObstacle", startDelay, repeatRate);
Instantiate(obstaclePrefab, spawnPos, obstaclePrefab.transform.rotation); }
void SpawnObstacle () {
Instantiate(obstaclePrefab, spawnPos, obstaclePrefab.transform.rotation); }
Lesson Recap
New ● Player jumps on spacebar press
Functionality ● Player cannot double-jump
● Obstacles and Background move left
● Obstacles spawn on intervals
Next Lesson ● We’re going to fix that weird effect we created by moving the background
left by having it actually constantly scroll using code!