Daily Progression — getting the player to jump
Last time we programmed our player to jump using a character controller. This time, we’ll be using the Rigidbody2D component. We’ll need to check for the player to be grounded, but let’s start simple. Let’s get the player jumping.
After doing a bit of preliminary research, it seemed AddForce was the way to go, AddForce also has various modes: Acceleration, Force, Impulse, and VelocityChange. Impulse seemed to make the most sense, so let’s see if I chose correctly:
This is just one way of jumping, another way is changing the velocity of the rigidbody:
Looking good! but the player can jump as many times as they want, we can limit it to one jump, but that brings us to our next challenge: Knowing when we are grounded. We’re going to need to use RayCast for this.
A visit to https://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html should give us the info we need:
So when using RaycastHit2D, we’ll need an origin point, a direction, and distance, and a variable of type RaycastHit2D to put it in.
I’m going to create a new function, CheckIfGround, we’ll also call it in the update.
We’ll also put a check in the jump as well:
If we try it right now, we won’t be able to jump, because the raycast is more than likely detecting the player. We can see this if we ask it.
and sure enough:
While the ground tiles are tagged ‘Ground’, Raycast does take advantage of layers so we can give the ground a layer a special layer name so that we can tell raycast to ONLY check that layer! So with the ‘floor’ tiles selected, I drug down the Layer menu to create a new one, and in layer 8, created a new Ground layer.
A word of warning here, this particular situation isn’t as easy as putting 8 in the raycasts layer as it’s asking, you need to use something called ‘bit shifting’ in order for this to work correctly, and it’s actually an advanced computer science issue that people can earn PHDs in, and not a Unity issue. Suffice to say, just accept this is the way.
Now let’s check:
That’s what we wanted!
An optional way is to create a holder for the ground layer, let’s say _groundLayer as an example variable name; use SerializeField on it to make it available in the inspector. Then in the inspector choose the layer in the dropdown menu, and once that’s done. instead of the bit-shifted number, use _groundLayer.value instead. either way is a bit quirky, admittedly, but it’s just one of those Unity things.
By the way, it would be nice if we could see the actual raycast. Unity comes to the rescue again with Debug.DrawRay:
it’s very similar to the raycast so let’s put the drawray under our raycast.
Now we can see it in our scene! You can also see it in the game by clicking on the Gizmos button above the Game area.
That said, now if we check our _isGrounded variable in the inspector, we can see the raycast is working perfectly now!
If you can double jump at this point, it could be your raycast is too long. Feel free to shorten it to the point it’s just barely touching the ground. It’s a process of trial and error and will be different for everyone. I ended up changing my _raycastDistance variable to 1.1 and that seemed to work fine.
We’ll give our ray check a bit of breathing room though, but for now, we’ll call it an article. :)