Day 104: Creating A Ledge Grab In Unity Part 2
Objective: Create the logic for the Ledge Grab.
To get started with the logic, we will need a script on the ledge that we will grab on to. I have named it LedgeGrab.
In that Script we will need a reference to where the hands will need to be. After that we will need to use OnTriggerEnter to watch for the players Ledge checker.
Before we move on we will need to set the player’s position when the Ledge Checker enters. We will move back to the player controller and create a method that will set the players position to equal the hands position. This method will ask for a vector3 for hand position and a LedgeGrab.
Now that we have a method to set the players position to be hanging from the ledge, it is time to set that position. In the LedgeGrab script we will pass in the handpoisiton and the ledge grab script.
That is all for the LedgeGrab script for now. We will move back to the player script and let the game know that we can no longer move using our CharacterController. In the method we just created we will set a new bool to true. We will also disable our CharacterController.
This is all we need to just grab on to the ledge and just stay there. Let’s let the animator know it can player the hanging idle animation now. In the animator we will add a new bool parameter called HoldingLedge.
We will transition from the Jump animator to Hanging Idle animation.
Now that the transition is set up with the correct condition, let’s have the script set the bool to true. In the GrabLedge method we created, we will set HoldingLedge to true, Speed to zero, and Jumping to false.
Now when we jump to a ledge and our LedgeChecker interacts with the Ledge, we will be attached at the position of our Hand Position and we will just hang off the edge. Tomorrow we will cover how to climb up from the ledge and set the players position to the new spot.
Thank you for reading and have a wonderful day!