Creating a Ledge Grab System (part 3)
Snapping in place and Pulling your character up.
Now that we have our hanging animation in place we need to set up the position so it doesn’t look like our player is hanging in mid-air. To do that we will need force the position to the player so it snaps in a desired location when hitting the edge.
Scripting the Snap:
In the ledge script we need to make an serialized field variable to handle the vector3 position we want for our player to snap too. We need to serialized it so we can access this data within the Inspector for ease of adjustment later on.
Next we need to force the placement of the player only when the player collides with the ledge checker, so within the IF statement under the On Trigger Enter method, we will add a the ledge position variable we just created earlier.
In the player script we need to apply the ledge position in the public method by using Vector3.
Then we add the forced position within the method, by using Transform.position = _LedgePos.
Now we will snap to once place when our colliders make contact.
Adding an Extra Animation:
As an extra step, I personally decided to add in an extra animation during the jumping and ledge hang, and that is the bumping of the character when making first contact.
I used another animation aided by Mixamo, which I downloaded and imported to the Animator.
In the Animator, I used the jumping to hanging animation before the actual hanging animation. I set the conditions that used to belong to the hanging state to the Jump to hang state. Then the Hanging state is just a non-condition transition. Just make sure that the jump to hang is not set to loop, since we only want to happen once then transition to the hang.
The end result should look very smooth, if you find that the position is slightly off, adjust it within the Ledge checker where we set the Vector3 variables.