Day 18: Making our player grab a ledge and snap to a good hand position

Objective: To have our player detect when we are about to grab a ledge, play the appropriate ledge grab animation, and snap to a good position that makes it look like our hands are grabbing a ledge
Now that we’re ready to flesh out our ledge grabbing system, we’ll need to set up a few things to get started. First we need to place a box collider object inside of our player object. We will call this our Ledge_Grab_Checker and we will tag it with the same name.

This object will will be used to detect when we collide with a Ledge_Checker object so that we can access a public method in our player which will freeze us in place and snap the player to a good position. Speaking of the Ledge_Checker; we need to have this as a box collider which is set to trigger. These need to be placed on the edge of a ledge and these objects will have a script attached called Ledge. This ledge script will simply strore a vector 3 that we can use to snap our players position and it will use the OnTriggerEnter(Collider other) method to pick up the Ledge_Grab_Checker by it’s tag.


The ledge script will only use the OnTriggerEnter method and that’s it. once we detect the ledge grab checker then we will grab the player component from the parent transform. This will helps us to call a public method in the player class to turn off the player controller and to set off the bool needed to play the ledge grab animation. This method will take in a vector 3 parameter which will be stored inside of our Ledge script.

Inside of our player script we simply have to create the GrabLedge(Vector3 _targetPosition) method. We already have hooks into our player controller and animator so we should have what we need to get this mechanic started.

Setting the _targetPosition for our ledge script will require us to test out the script first by colliding with the ledge checker object. Then we will un play unity and use those transform.position values and assign them to our Ledge script in the inspector.

Once we collide with a ledge checker we can pause the game and move the player object to a nice position that makes it look like the hands are grabbing the ledge. Copy those values and paste them into the _targetPosition field and now whenever we collide with the ledge checker we will snap to a good position. The full effect can be seen in the gif at the top of this article.
Now that we have the first part of our ledge grab mechanic built, we can move on to the second part which is climbing up from the ledge.