My journey becoming a Unity game developer: 2.5D Infinite Runner-Ledge Grab System-Pt3
Objective: To make the Player jump, grab the ledge, and hang on in their Hanging Idle animation.
We want to now have the Player’s hands attach to the ledge to make them look like they’re hanging from the ledge. Let’s create a target for the Player to synchronize with.
Right-click on the Ledge-Checker game object, and create a Cube object name Hand Position. Scale it to match the edge of the ledge, and position it for now in front of the ledge.
We adjusted the thickness of the Ledge Grab Checker attached with the Player, and the Ledge Checker to make the Player have a thinner area for the Player to lock in on.
In the Ledge script, create a new variable accessible in the Inspector Type Game Object name handPos. This will represent the Hand Position game object that the Player will align itself with. Then we will pass in the handPos variable to the GrabLedge() method for the Player to align with.
In the Player script, we will assign the Player’s position to equal the Hand Position game object’s position.
When we play the game, notice that the Player is at the Hand Position’s position, but they’re hanging above the Hand Position object. We moved the Player downward to show the it’s aligned with the Hand Position object. Remember this situation as we will have to do something from this position.
We will try another approach to solve this situation by using the Player script, and passing in another argument with a Type Game Object name ledgeChecker. We will set the ledgeChecker’s position to equal the position of the handPos game object.
Back in the Ledge script, create another variable Type Game Object name ledgeGrabChecker. We will assign the Player’s position to equal the ledgeGrabChecker object’s position which is a child of the Player. Then, pass in the ledgeGrabChecker which represents the ledgeChecker argument with the handPos to the GrabLedge() method.
The LedgeGrabChecker object is aligned with the Hand Position object, but the Player is still hanging above the LedgeChecker object instead. The LedgeGrabChecker detached itself from the Player technically, but it did align itself to the Hand Position.
Remember the LedgeGrabChecker is a child of the Player, so it can detach itself from the Player and move to the Hand Position object position. While the Player who is the Parent object doesn’t have the commitment to follow a child object. Therefore, the Player is going to freeze in its Hanging Idle animation at the LedgeChecker game object’s position.
We need to move the Player down to the position of the Hand Position object. To do this, we will need to create an Offset to the Player’s position.
Back in the Player script, we want to set the Player’s position to equal the Hand Position object, but we will add a new Vector3 position. We will use the Player’s X-axis, but change the Y-axis to come down to the Hand Position object position which in this case for us will be -4.7f. The Z-axis we’re going to move back by -0.6f from the Hand Position for a reason we will see shortly.
Make sure to remove the ledgeGrabChecker variable from the GrabLedge() arguments and variable field as we won’t need it anymore.
Watch the Player now snap into the position of the Hand Position, but with a difference in the Z-axis. The reason we moved the Player away from the Hand Position object was that the Player object was going through the floor game objects. This is something that we must keep in mind when doing ledge jumps with respect to the Main Camera’s angle in the game.
We moved the Player ledge grab along the Z-axis a little closer to the Hand Position object by .03f. Notice my leg going through the floor, this is something that we have to deal with when setting up our gameplay.
There might be times where we can change the asset to match the depth of the other asset to create a level ledge. However, you see my character belly going through the assets.
Therefore, with the Mesh Renderers turned off on the LedgeGrabChecker, LedgeChecker, and Hand Position just play around with the Player’s position. But, just remember to adjust the Player with respect to the Main Camera, so the Player don’t look weird hanging on the ledge. Remember, the user may not be able to see if the Player’s hands are on the ledge, use this to your advantage.
Next time we will work on the Player climbing up and off the ledge to a standing position.