Creating a Ledge Grab in Unity Part 2!
Objective: Create code to detect when the two checker objects collide and start the ledge grab functions.
In the last tutorial we set up two objects, one for the player and one for the ledge and we want to have the ledge check for a collision with the player’s checker. Once the collision happens we want to position the player to hang from the side of the ledge.
Start the editor and pause it. With the player in the hanging position drag your player to the ledge and place them where you like the looks of them hanging. When decided, write down the players X, Y, and Z values. Turn of the editor.
Create a new C# script and attach it to the ledge checker object. Inside this we can delete both the start method and the update method we wont need them, this is a trigger script.
Create a Vector3 variable to store the position you wrote down earlier. Serialize the variable and in the inspector enter those values.
Now, you need to send these values to the player. Create a method on the player’s script with a vector3 parameter. Inside this method we want to set the players position to the vector 3 parameter.
We also need this grab ledge method to disable the controller while the player is “hanging” on the ledge. We also need to set the animation parameter speed to 0, and jumping to false.
We need to delete the transition from any state to the hanging animation. We need delete the trigger “Ledge Grab” and create a bool. Using a bool give us better control over the animation transitions. Create a transition from your jump animation to the hanging animation with the condition being the grab ledge bool is true
Now that the ledge animation is activated turn off the transition duration on animation. You can now adjust the position of the ledge checker for a smoother looking animation.