In this lecture, we need our character to jump.
So we're going to implement the code for jumping and do a bit of tuning.
Now we could add a jumping animation, but I'm okay just using the running and idling depending upon
what the player is doing.
I think that'll look fine if you if you jump in it still using the running animation that is.
Okay, let's have a little bit of a reminder in our input actions information.
We created a jump method or a jump action.
Sorry.
And this jump action automatically creates the on jump message.
The on jump message.
What's inside that?
Well, nothing yet because we haven't created it.
But we will create things that the game will do when on jump is called.
And when is it called?
Well, it's called when either of these two bindings is enacted.
So when space is clicked or when a button south is clicked enacted.
That's not a word I use every day.
Anyway, we've learnt something and so here is jump.
And as I showed you before, if you click on player and look for the player input, you can see that
one of the messages down in here is going to be on jump just there.
So over into our script, our player movement script, let's see underneath on move I will create void
on jump and once again we need a particular value to be passed into us.
We need to know whether the button's pushed or not.
So we will create input value.
Once again, we'll just call this value and then in our curly braces and on jump is a little bit different
to our on move and our run.
So on move.
We wanted to know what's the vector two and because it doesn't change every frame it might stay on,
say, plus one while the player is holding down a key.
We needed a way of storing the plus one up here in our move input and then in update saying hey, continue
to run if the circumstances are appropriate.
In other words, it's greater or less than zero, but with jump it's a button, so we don't need to
be putting this into update.
It literally is when the button is pressed, do a thing.
So we're going to create a bit of an if statement, if the value dot is pressed.
That's our way of doing it up here.
It was valued at get vector too, but this is valued is pressed.
If it's pressed, then do some stuff.
Do stuff.
I'm giggling because I was about to write do stuff which is incredibly sophisticated programming.
Starting Point.
We know we're working with the rigid body and also with velocity, and this is why we want to add a
force to it.
We want to throw some velocity onto this.
So I'm going to plus equals.
So take whatever your velocity is right now for up and down and then add something to it.
And what will we add to it?
Well, we'll create a new vector to once again, we need an X and a Y on the X.
We don't want to be adding anything.
So I think we can just say zero on there.
Don't add a new thing with run we were assigning.
So the velocity is now this particular velocity, but within the jump we're adding.
So we're saying take what you've got and add some more to it.
We don't want to add any more to the X, so we'll leave that at zero and then to the Y we want to add
a certain amount.
So I don't know what that certain amount is yet.
So we'll call it jump speed as a new variable semicolon and we'll jump up to pardon the pun, jump up
to the top serialised field and we'll make this a float and jump speed.
Initialize this at five, maybe five f semicolon back down here.
So jump speed should be cool.
Now let's save that and see if it actually works.
Click on play jump is assigned to spacebar.
Bu there we go.
We are jumping.
Whoa.
And that is some serious distance we get with our jump just there because we're floating in the air
for quite some time.
So that's a win.
Let's have a look at some of the issues in here.
If I was to push the spacebar twice, once, twice, three, four, five.
Okay, so that's an issue we'll need to address in upcoming lectures.
What else can we do?
Well, we're all right.
We're going a really long way.
That might be what you want, but it's not really what I want.
And if we fall down here, could we actually get out of there?
No, we couldn't.
So there's some things we need to tune for our character.
Let me show you show you a couple of things, and then you can go ahead and tune this yourself.
The first thing, if we go up to edit project settings and look for physics, 2D within here we've got
a gravity setting so we can make the gravity heavier for the player.
We can make it lighter.
That's one place we could change it.
And this will change everything in our game world.
It'll change things.
We shoot, it'll change enemies.
If we change our gravity here, that's a holistic gravity.
Or we could click on our player and then find within our rigid body.
You can see the gravity scale for the player just here.
And if we wanted to make the world heavier or lighter, we could change it in here.
And so if you were changing the gravity for everything in your world, then you'd certainly want to
go into the project settings and the physics 2D in there.
So it changes for everything.
But I'm pretty much only going to care about gravity for my player, so I'm just going to do it directly
in here, make the gravity twice as strong, and then see how that goes.
So gravity scale of two, okay, that's pretty good.
But now he's not jumping anywhere near high enough and that's going to be changed within the player
movement script.
And you can see our jump speed is five in here.
So it's time for a challenge I think.
Tune your jump using the gravity and jump speed tune your jump so it feels good.
Decide how many tiles you want your player to be able to jump up.
So if I run my player at the moment down to this little bit where I've got the jump, ignore the water
for now, it'll do some watery stuff later on.
But down here I can only jump maybe one tile.
I can't even get out of this.
This is two tiles in here.
One, two.
I think I want my character to be able to jump a little bit over three tiles.
So that's the tuning I'm going to be aiming for and then decide how far you're okay with your player
floating horizontally when jumping.
So all these things are combined to say, Well, what should I tune it?
To tune it based upon the performance you want your player to have.
So there's a challenge to take that on.
C back here when you're done.
Okay.
First thing, I need my character to jump higher.
So Jump Speed will put this to 15.
Click on play, see what happens, but jump down here.
But that's now far too fast.
So they make the gravity heavier or the jump speed less.
I think I'm going to try the gravity heavier while I'm still in plane mode.
I'll do this so I can change these things on the fly.
That's pretty good.
It's floaty.
It gets quite a long distance, though, doesn't he?
So jump speed will put to 20.
Gravity will plop up to four.
See how that impacts blow?
He's flying way too far.
Gravity will put to ten just to be crazy.
Okay.
And there's a good issue that we've spotted just there that he fell right through the world.
So I'll get back to that in a moment.
Just mental note, I think maybe eight.
So eight on the gravity scale and I click out of play mode.
It's going to go back to what it was eight and 20, I think I had here jump speed.
We'll see how that goes and I'll play around with that a little bit more.
But we just want near enough is good enough for the moment.
The other thing was with the player missing the collision, that's because we currently have our collision
detection on discrete.
We want to change that to continuous.
Continuous is better for things that are moving a lot and moving quickly.
It's a little bit more expensive computationally.
So we only want to do that really for the player character and anything really important.
Pop that on to continuous.
We'll see if we still have that problem.
All right.
This was the spot.
Looks pretty good.
I like the speed.
Now the player can get a reasonable distance.
It feels pretty good, actually.
The only test I've got now is can he reach the third level?
So I just need to make the jump speed a little bit more than I think I'm done.
So 25 but I'm going to put it somewhere around 25.
My gravity I think was somewhere around four, wasn't it?
So there we go.
That's feeling pretty good.
Like Bing's like a little jumping.
Jack I kind of like the feeling, but you can have yours tuned however you like that works for you.
Mine's at Gravity Scale eight and Jump Speed 25.
So there we go.
We've implemented basic jumping into our game.
There's a few more things that we need to resolve with jumping, couple of bugs, but we've got the
basics for it.
Okay, so in the next lecture.