How to Jump in Unity - Unity3D Fundamentals

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
In this video I will show you how to add a simple jump functionality to our beloved cube you know just like that starving alright first let's create another cube and we're going to name it as platformer let's scale this but first let's reset its position let's just scale it have y value to 5x + 2 you z so let's drag it a little more going to give the health half of Y value of scale so that it will stand on the floor right to make this cube jump we are going to give a little push to it you know whenever the space key is pressed we're going to add up force in the upward direction to the cube so for that the rigid body components luckily we added it in the previous tutorial when we learned about the colliders right to reference this rigidbody component I let's initialize it usually as RB and we're going to write a start method in there we are going to reference 2d game objects rigidbody with this line of code is referencing to the rigidbody that this script this whole you know cube movements script is assigned to which is player argue as he reached buddy all right so this provides a simple function name ad force alright and this takes two arguments one is a way to free object and the other is a force mode which will use the impulse mode functionality all right let's give five to the Y value and zero to the other ones let's save and return to unity now if you press play so as you see frame this force is added to the cube but we don't want that we only want whenever the space key is pressed so for that we are going to use we are going to initialize an if statement we call the input and gets button-down function or method are going to pass the jump key which is space and you can see that in the edits project settings input section that and there's a jump axis signed keys space and there are also you know gamepad keys or this alright now let's create our brackets and move this code to the if inside the if statement alright let's save it return to unity right as you see when we press space jumps but the problem is when you press space on the air now the force is applied again and again now it's just like us flappy bird all right this is fun also but for this video we don't want that we just want only one jumping not multiple for that's we're going to use colliders and we saw how that a colliders work in the previous tutorial by basically you know we will check whether the floor is on the ground or not let's initialize a public boolean variable as you know bullying's are returned either true or false and will name his ass name it ass cube is on the ground the ground and the initial value will be true all right so whenever we press the jump key we will make this variable false but we should also check this variable if space if statement whether the cube is on the floor or not do that we're going to now write this end operator and we are going to pass this variable you can also do like this true but don't have to okay this is our first logic the second is we are going to make this variable true again when the cube is landed on land on floor for that if you recall from the previous tutorial you know in the collision control there is a method name on collision enter this is called the first moment the tube game object has collide all right we're going to call this on collision entered inside this we are going to check whether the cube landed on the floor that's collision gameobjects will get its name and if it is the floor then we are going to turn this variable variable into true all right let's say that return to unity and if we check it now you'll see now it only works once alright good now but what happens when we landed on platform on this platform here let's jump on it can we jump back again on the floor no hit press and you'll see no we can't jump now and this is because we are checking the name of the game object that we're colliding with and we're seeing we're checking if it's name is floor or not but this object's name is not floor it's platform but we can't name the same thing every game object that we want to land on you know and for that reason we are going to use tags this tag here you can click on this tag drop down and the Ground won't be initialized for you when you first open this up because I already initialized it we were going to press the add tag and you know type ground and click Save won't do that because I already did it and after that you will see the ground option here alright go ahead and tag it tag the floor as ground and plot platform as ground also so we'll change this as tag and we'll change this as ground let's test it now let's hit play alright jump on floor but can we jump on the platform alright yes we can jump on platform very good alright in this video we added a simple jump functionality to our cube thank you for watching and I'll see you on the next videos bye
Info
Channel: Deniz Simsek
Views: 136,020
Rating: undefined out of 5
Keywords: jump, unity, unity3D, platform, rpg
Id: xvLMD2qWaKk
Channel Id: undefined
Length: 10min 14sec (614 seconds)
Published: Sat Jan 19 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.