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: Creating the jump function
See Details
Joshua Kinney


Joshua Kinney
In this lesson, we're going to create some jump functionality for our movement script. So let's go ahead and create our new lesson. So we're going to create in our script folder a C# script. And let's call this Lesson_13_Movement. And we're going to double click on that to bring out Mono Develop. And let's go ahead and take our Lesson 12 script. And we're going to copy that and paste it in Lesson 13. So make sure we change the name of that class. And then I'm also going to get rid of some of these comments here, just this one here. It's not needed any longer. So just a little bit of clean up there. All right, so now that we are started here, let's go ahead and save this. And let's get into creating our jump functionality. Now we started working with Vector3's. And we had talked about using the x-direction, which is this horizontal axis. And then we also talked about using the z-direction, which is our vertical axis. Now we're transforming along the Vector3 by using the shorthand of dot right to move left and right. And then we're also using the shorthand of dot forward to move forward on the z-direction, or z-axis. So what we're missing is our y-direction. And that will actually allow us to jump up in the y-direction. So to get started with this, I want to create functionality to allow my player to hit the space bar to jump. So what I'm going to do is I'm going to go right below my Vector3's here. And I'm just going to give myself some space. And we're going to put in an if statement. And we're going to say if the player inputs the space bar key, then we want it to call the jump function. So already you should be getting some ideas on how this works. So we're going to type in if. And then we're going to say input dot get button down. We want to put in our jump function. So we could type in space, or we could use the input manager. So the input manager is going to allow us to create button configurations. And it's really a good idea to get acquainted with the input manager. So let's go ahead and bring it up by going to Edit, Project Settings, Input. And then here we see that we have horizontal, vertical, and then we also have jump. Now the jump is set to the space bar, and no other negative button. And there's no alternative button for that. So it's going to be just the space bar. Let's go ahead and input its name into here. And we'll use it. Let's make sure we close all of our parentheses for this if statement. And then let's give it the execution code. So we're going to type in jump. And we're going to call that jump function. Now we haven't created that jump function just yet. So let's get into that. So I'm going to go ahead and go just below. Well, actually, let's remove this right here. Let's just cut that. And let's put that above our jump if statement there. So let's paste that back in. Now what I want to do is go ahead and create that jump function. So let's go ahead and type in void jump, open close parentheses. And then we're going to open close curly brackets. And then let's start to input our code. Now in order to get our object to jump, we need to think of how we're going to control this object. The way that we could go about this is going in and looking at our reference and getting some ideas of this. So I have Vector3 here. And we could look through some of this. And it really isn't going to give me much of a clue on where to go. But what you can do is you could start to search through some of these different runtime classes and get some ideas that way. So if I'm looking through some of these, you'll see that I have character controller here. And I'm going to go ahead and click on that, and just kind of get a description of what this looks like. Now I'm just showing you a workflow for someone who really isn't all that acquainted with scripting in Unity. This is the best way to get experience is to go through some of the documentation here in your scripting reference and start to play around with some of the different scripts, do some experiments. And before you know it, you'll really start to learn how to code. And you'll have an idea of how to create different types of script. It's just like building up your vocabulary. You have to learn the word. You have to learn what the word means. And then you have to use it in a sentence. So that's kind of the basis of what we're taking here to learn scripting. So CharacterController. Let's learn what it means. "A CharacterController allows you to easily do movement constrained by collisions without having to deal with a rigidbody." Now I could use this CharacterController here. And I would need to input a CharacterController into my scene. But I'm just using a basic cube right now. And I want to use the CharacterController on a different portion of the specific course. So we're going to come back to this. But one thing that I do you see here is I see this rigidbody. So let's go ahead and check it out and see what that means. So let's type in rigidbody. And let's go ahead and click on that. And you can learn what that means. So it controls an object's position through physics simulation. So this is pretty neat. We can create a physics type of jump, so that way we don't necessarily have to create gravity and that sort of thing. All right, so let's go ahead and move into Mono Develop. And let's try out that rigidbody scheme there. So I'm going to type in rigidbody. And I'm going to do dot. And you can go through IntelliSense here and start to look through some of these different functions and classes here and some of the different variables and start working at that. So this AddForce is what I want to use here. And what I'm going to do is tell it how to add force. So what direction do I add this force? Now you'll see we have Vector3. And it's asking for the force itself. So what I have to do is I need to give it a direction. So I'm going to say Vector3.up. So that's that shorthand there, as you can see here. And then we're going to multiply that by the actual force itself. So our force, we're going to create a variable. And we're going to call this jump speed. All right, let's end that with a semicolon. And we need to go ahead and create that variable. So right up here underneath the speed, let's type in public, and we're going to use float, jump speed. And we're going to set that to 20, 20.0. And then remember, float values need to end with an F and a semicolon. So now that we have that, let's go ahead and save this. And let's go into Unity. And it says we have an error here. It says keyword void cannot be used in this context. Let's see what we're up against here. It's saying void jump, does not like that. And the reason that it doesn't like that, looking at this, I've actually put this jump in our avoid update. And we can't have that. So let's go ahead and grab this, let's cut it out. And let's go ahead and move this up. And here is our close parentheses for our update. And I'm going to go just below that. And I'm going to hit Control V. Let's paste that back in. Now this should work just fine. Let's go into Unity. Our errors disappear. Let's go ahead and go to our cube. Let's remove Lesson 12 and apply Lesson 13. Now if we hit play, we can still move around. And then if we hit the space bar, you'll see here that it's saying there is no rigidbody attached to the cube. Well, this is an easy fix. Let's go ahead and close this. And let's go up to Component with our cube selected. And we want to go to Physics, Rigidbody. Now that rigidbody is attached to this object. And now we can hit play. And now we can hit the space bar, and our box will begin to jump. Now you'll notice that it's not moving. And that's because our jump speed isn't high enough. So let's take this up to 100. And then let's hit space bar. And you'll see that we're jumping. Now if you want to jump higher, you just increase that jump speed. And let's go in our viewport here. Now one thing that you'll notice is that I can keep hitting that space bar, and it will raise my box up into the air. So that's going to be an issue. Now another thing that I have here with this rigidbody is that it is working with Physics. So it's going to knock this box around. And if I start to move and jump at the same time, it's going to flip around and start changing directions. And then it's very hard to control. So what we need to do is we need to improve our jump script here by constraining it to one direction. And then we also need to work on the is grounded functionality. So we only want our box to jump if it is on the ground. We don't want it to do a double jump or anything like that. So let's go ahead and get started with 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.