Ledge Grab Part 1
Objective: Create A Ledge Grab in Unity
Currently our Player can run and jump to the ledge but he remains in the above transition. We need to fix that by allowing him to grab the ledge. As like with the other animations, we go to Mixamo and find the aniamtion we want to use.
We do the usual import, change rig to humanoid, duplicate animation, put it in the animations folder, turn loop time on and bake into pose.
We will use a cube object with a trigger as part of the Player to use for a trigger. There will be another cube object set up to act as a ledge trigger check when the players trigger cube hits or collides. This will then be used to trigger the ledge hang animation and logic for the Player to hang from the ledge.
We will fix the position of the trigger objects later once the code and animations are working. Create a script for the ledge and attach it to the fixed cube trigger. Add a Rigidbody to the Players cube as one of the objects colliding needs a Rigidbody for the OnTriggerEnter to work. Inside the Player script, move all the movement logic from the Update method into its own method called CalculateMovement and call it in the UpdateMethod. We will turning off the Character Controller on trigger so we will run an if statement to check if the controller is enabled before running the CalculateMovement method.
Next we need a public function for the Player to grab the ledge and this will take in a Vector3. This Vector3 will be the Players position when hanging on the ledge.
Move in to Ledge script and create a serialized Vector3 for the Player ledge position. We can find this through the scene view by moving the Player into place taking the transform positions and then entering them in the Inspector as values for the Vector3. Next we need an OnTriggerEnter and check if the other tag is the ledge. If so get the Player using other transform parent GetComponent and null check before called the Player class method GrabLedge and passing through the Player ledge position.
Next set up the transition between running jump and hanging idle in the Animator. To do this we add a bool parameter of grabLedge and when true transition to hanging with no exit time, no fixed duration and 0 transition duration.
Back in the Player script, inside the GrabLedge method we can now set the controller to disabled, trigger the ledge grab animation and set the Players position to the Vector3 values that were passed through.
In the next post, we will get the Player to climb up from the ledge.