Important Notice:

On July 31st, 2017 the Digital-Tutors library will be retired along with Digital-Tutors concurrent licensing. Effective August 1, 2017 any active group plans will be redirected to www.pluralsight.com. Each group plan will receive one individual user license on Pluralsight for each active Digital-Tutors license. Please contact your account representative for any additional questions.
Creating the jump functionality
See Details
Joshua Kinney

Pluralsight

Joshua Kinney
In this lesson, we're going to apply gravity and create our jump script. All right, so in the last lesson, we had created our move script, but we had figured out that we had a logic error, where our character controller, if it's not touching the ground, it will not move. So to do this, let's go ahead and create some gravity to get our move working, and then we'll go ahead and create our jump. So go ahead and create a Lesson_17 movement script, and copy over everything from lesson 16. And it should look just like this. All right, so to get started with this, let's create our gravity variable here. So I'm going to make this a public variable, so that way I can adjust it. And we're going to make it a flow data type. And we'll give it an identifier or name as gravity. And let's set a value. So let's just to 20.0, and make sure that we have that F at the end for our float value. All right, now, once I have this, I need to come in here and actually add to the gravity. So gravity, all it's going to do is, it's going to take our move direction, and it's going to move it in the y direction. And we want it to pull down in the y direction. And we want that to be by the value of gravity, which is our variable. And we want it to pull down according to the time-- the actual time, and not per frame on the computer. So let's go ahead and move down into our update function. And let's just-- right down here-- let's add a comment here. And let's say "Apply gravity." So inside of here, let's type in move direction, and we're going to say .y, which is the direction we want to move in. And then we want it to move in the negative y direction. So we want the move direction .y to equal move direction .y minus gravity. So minus equals gravity. We want to multiply that by Time.deltaTime, so that way it will move at the correct speed. All right, so now we have that, let's go ahead and save this. Let's go into Unity. Let's apply this to our player object. So go to Player, remove lesson 16, and apply lesson 17. Now you'll see that we have speed and we have gravity. Let's hit play. And now you can see that we can move left and right. And we could also strafe. And this is because our character controller is moving. So to see this in action, let's actually grab our player object. Let's move it up in the y, let's hit play, and you'll watch him drop. So our gravity is working. All right, so I hit play on that. I'm going to hit Control-Z one time-- or a couple of times to make that go a little bit closer to the ground there. All right, so now we have that, let's create our jump functionality. So inside of MonoDevelop, let's go ahead and create a variable. And we're going to make this jump speed-- so public float jumpspeed. And let's go ahead and set this to something, like, 8.0, just a good, round value here. So 8.0, with that F at the end to denote that is a float value. And now we want to move on to getting our actual jump functionality. So I'm going to go into my update controller, or update function. And we're just going to use a simple if statement to say, if the jump button is pressed, move direction equals jump speed, all right? So if our character is grounded, so we want to use it inside of this if statement, and let's go ahead and create this here. I'm going to give myself a little bit of room. I'm going to say if, and then I'm going to say input-- oops. Let's try one more time. If input .get button. OK, we're going to say jump, and double parentheses on that, to make sure it's opened and closed properly, and then we're going to put in our directions for our if statement. So we're going to say move direction .y, So we want it to move in the positive y direction, and we're going to set that equal to the jump speed. All right, so now that we have that, we should be able to go ahead and just hit the Space Bar, and it will take that in the proper direction there. So let's save this and test it out. So let's go ahead and go to Play. You'll see that our jump speed and our gravity are still here. We can move. If we hit the Space Bar, you can see that we can jump. OK, now one thing that I want you to notice is I've coded this in such a way that we don't want that Mario Brothers type of control. So if I jump, I just want to be able to jump up, and then that's it. I don't want my direction to really control that while I'm holding the button down. OK, so we can jump. Now, the only problem that we have here is that our player is not an animating. So what we want to do in our next lesson is, we want to show you how to create some scripts that will actually control the animation of the player as we move. So I'll see you then.
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