Ledge Grab System: Part II
In this article, we will continue to build upon our ledge grab system by creating a game object attached to the platformer that will detect the ledge grab checker we attached to our player in Part I.
In the platform game object we are going to add a cube game object and name it Ledge_Checker.
We want to position it to where we would want the Ledge Grab Checker to collide with the Ledge Checker, which will trigger the Hang Idle animation. The position will need to be tinkered with until we are happy with how it looks.
When these two objects collide we will need to identify them, so we will tag the Ledge Grab Checker.
Also, for the Collision Trigger to work one of the objects will need a Rigidbody so we will add one to the Ledge Grab Checker.
We are going to create a script for the Ledge and use OnTriggerEnter to detect when the Ledge Grab Checker game object has collided with the Ledge Checker. When that happens, we will want to access the player controller and disable it so it turns off gravity.
To access the player controller we need to get a handle on the Player script. We’ll create a variable called “player” and get the Player component via the Collider other. We need to go through the transform and the parent of the Ledge Checker to get the Player script since the Ledge Checker is its child.
In the Player script, we clean it up by moving the movement code into a method.
We will create a new public method called grabLedge. We want it to be public so we can call it from the Ledge script. This method will simple disable the player controller which will stop it movement.
Back on the Ledge script, we will null check the Player component and if we grabbed it we will call the GrabLedge method from the player.
For the animation, we will move the Hanging Idle animation from Any State to Jump. Also, we will delete the Trigger parameter and replace it with a bool parameter. The condition to have it transfer from the Jump to Hanging will be the bool parameter GrabLedge be true.
All we have left to do is set the parameter true in the script. We will do this in the GrabLedge method.
It looks rough now, but the goal was to have the collision between the two ledge detectors, to stop the player in mid air and play the hang idle animation. In the coming parts, we will be able to pull the player up by pressing a key and resume player controls.