Important Notice:

As of September 15, 2016, we no longer publish new courses on this website. We encourage you to use your Digital-Tutors credentials to sign in on Pluralsight where you'll find all new creative courses, skill tests and paths, 1:1 mentoring and more.
Project: Refining the jump function
See Details
Joshua Kinney

Pluralsight

Joshua Kinney
In this lesson, we're going to refine our jump functionality to only jump when it's grounded. All right, so let's go ahead and create the Lesson_14_Movement script. And I've already done that and brought it in two MonoDevelop. And I copied over the script from lesson 13 and pasted it into lesson 14, and then also changed the class name here to match our script name. Go ahead and save that once you've copied that in, and let's get started here. All right, so the issue that we're having here is that whenever we press the jump key and we continue to hit that space bar, what happens is it will go ahead and jump even though it's not grounded. And that's not necessarily something that I want for my game. So to take care of this, let's go ahead and start looking at some possibilities to make our rigid body stay grounded. So let's go ahead and bring up our Scripting Reference. And we have Rigidbody here. And if we take a look at some of the-- let's go ahead and just go back here. If we take a look at some of the different variables and functions that we have, we might be able to get something. So what I want to do is I want to be able to detect collisions with my rigid body. So I know it has that capability. So I have detectCollisions. So should collision detection be enabled? Well, I want it to be on. So that's not necessarily something that I'm looking for. Let's see. Let's look for something else, a little bit different. Let's see. We have this Message Sent. It says OnCollisionEnter. So this says OnCollisionEnter is called when a collider or rigid body has began touching another rigid body slash collider. Now, a collider could be any object with a collision on it. So that plane itself already has a collider. So that's going to work for us, so let's take a look at how this is written. So this is actually written as a function. So I could actually call a function, OnCollisionEnter, and have it detect collisions on my object. So I think this might be the way to go. So let's go ahead and experiment with this inside of MonoDevelop. So to get started here, a couple of things that I want to do is I want to have my jump only jump when it's grounded. So let's come in here to our jump function, and let's give it an if statement. So let's say if, and we're going to say grounded is equal to true. Then what I want it to do is go ahead and add the force, our jump speed. So I'm going to go ahead and put in our curly bracket for our if statement here, and just do a little bit of clean-up here, just spacing these over so everything looks good in our jump. Now I've created this grounded variable, so I need to go up to the top and actually declare that. So let's go ahead and move on up. It looks like I've lost some of my script here. There we go. So in our movement script, let's go up to the top. Let's type in public. And let's make sure that we space this over. And what I need to do is I need to determine the correct data type that I'm going to use for this grounded variable. So we're wanting to use this as a true or false. We want it to hold a true or false value. So the data type that I need to use is called bool. So that's going to allow us to input true or false data. So bool, and then we're gonna say grounded. And we're going to set that equal to true. So there we go. So now that we have that, let's go in and let's start talking about how we can create our collisions to make this work for us. All right, so let's go into our update function here, or actually below it, and below our jump function. Let's create a new function. So we're going to use that OnCollisionEnter. So let's type in OnCollisionEnter. Now this is one that comes with the API, or with our namespaces. So we don't want to change this in any way. Now, there's a specific way that this is written. Now, if you want to learn more about that, bring up your Scripting Reference, and you can see that OnCollisionEnter. And then it needs a collision info, which is going to be your identifier or your name. And then it's going to use the collision data type. So let's go in here for C#. We're going to type in our data type, which is going to be collision. And then we're going to give it its identifier or its name. And we're to say hit. So now we need to create our actual function. So let's define this function. And what we're going to do in here is we're gonna say, when it's grounded, or when we collide with something, we're going to set grounded to true. Now I want to send out a debug, just to make sure that this is working, so Debug.log. And I'm going to send out a message saying I'm colliding with something. And it's always a good idea to use these, just to check the functionality. So now I need to set my grounded variable to false whenever I jump. So whenever I'm off the ground, I want to set that to false. So whenever we hit the space bar, whenever we hit jump, let's go ahead and set our variable to false. So if grounded is equal to true. OK, we've set that here. So what I need to do is I need to set my grounded variable inside of this to false. So if it's set to true, or if this is true, then our rigid body is going to add the force. Once it adds the force and jumps into the air, we're going to set that grounded to false. And then it will not be able to be pressed again, because it's going to come back to our jump function if we hit that space bar. And if it's in the air, this is going to be set to false. And so this will not be able to run until we collide with something and that grounded is set back to true. All right, let's go ahead and save this. And let's go into Unity. Let's apply our lesson 14 script, so remove lesson 13, apply lesson 14. Let's go ahead and adjust our jump speed. So let's take that up to 100. And here you can see that we have the grounded variable exposed. And you'll see we have a checkbox here. So bool values, or variables, are represented by a checkbox inside of the Unity GUI. And whenever it's set to false, you will see that uncheck itself. So let's go ahead and hit Play. And if we hit the space bar, you'll see that that turns off and then turns back to true once it collides. You'll see that our message is applying there. And we could see this jumping. So I could hit this as fast as I want, but it will only jump if I'm colliding with something. Let's go ahead and take our jump speed to 200 here. And you can see now that that's jumping just fine. Now, the only issue that I have now is if I'm moving and jumping at the same time, it's really not wanting to work all that well. And it's flipping around. And what's happening is our axes are getting all mixed up. So I'm actually hitting W on the keyboard, and it's coming toward the camera. If I hit S, it moves away. that's the wrong direction. Left and right still work properly, until I get it flipped around again. And then it won't work. So what I need to do is I need to constrain my rotations of this cube to a specific axis, so that way it doesn't flip over. All right, so we're going to learn how to do that next.
In this series of Unity tutorials we'll discuss the major foundations of scripting with C# in Unity and apply what we've learned in two mini projects.

To start out, we'll look at several of the terms and techniques that are used when scripting in Unity such as creating and manipulating variables, understanding the different types of operators, and how we can create instructions for our game objects using functions. We'll also jump into creating logic with conditional statements, and loops. We'll even learn how to use basic arrays. Finally, we'll take what we've learned and apply it to creating a movement and animation script.
Introduction and project overview
1

Introduction and project overview

 
00:56
Basic C# Terms
2

Basic C# Terms

 
12:02
Creating and manipulating variables
3

Creating and manipulating variables

 
07:50
Working with assignment and arithmetic operators
4

Working with assignment and arithmetic operators

 
08:11
Working with comparison and logical operators
5

Working with comparison and logical operators

 
11:17
Creating logic with if statements
6

Creating logic with if statements

 
11:38
Creating switch statements
7

Creating switch statements

 
07:23
Creating custom functions
8

Creating custom functions

 
11:59
Working with loops
9

Working with loops

 
11:22
Understanding arrays
10

Understanding arrays

 
16:00
Project: Basic move script
11

Project: Basic move script

 
07:26
Project: Refining the movement script
12

Project: Refining the movement script

 
08:53
Project: Creating the jump function
13

Project: Creating the jump function

 
09:54
Project: Refining the jump function
14

Project: Refining the jump function

 
08:39
Project: Finishing the jump function
15

Project: Finishing the jump function

 
06:45
Project: Creating the advanced move script
16

Project: Creating the advanced move script

 
13:48
Creating the jump functionality
17

Creating the jump functionality

 
05:58
Project: Scripting basic animations
18

Project: Scripting basic animations

 
08:16