My journey becoming a Unity game developer: 2.5D Infinite Runner-Ledge Grab System-Pt2
Objective: Create a Ledge Checker game object that will detect if the Player’s Ledge Grab Checker object has collided into it. When it does, the Player’s controller will be turned off to keep the Player paused in the air after collision.
To start, let’s create a new Cube game object name Ledge Checker. Place it in front of the ledge the Player will jump to, and Scale it to the width of the ledge area that the Player will have to jump and grab onto.
Play the game and jump over to the Ledge Checker object. See if the Ledge Grab Checker attached to the Player collides into the Ledge Checker. If not, see if you need to reposition the Ledge Grab Checker object a little more in front of the Player’s Hanging Idle hands or Scaling the object to be a little wider.
In the Player script, we’re going to cut and paste all of the code in the Update() function and place it in a new method name CalculateMovement(). Then back in Update(), use the CalculateMovement() method to handle the Player’s movement just like before.
Also in the Player script, create a new method name GrabLedge() which will disable the Player’s controller by using the controller variable with the enabled() method being set to False.
Create a new script name Ledge which will use the OnTriggerEnter() method. Inside the method we will check IF the other game object’s Tag equals Ledge Grab Checker. If so, get the Player’s component which is a Parent by retrieving the game object’s transform of the parent. Check IF the Player isn’t NULL, and have the Player use the GrabLedge() method to pause the Player in mid-air after its Ledge Grab Checker collides into the Ledge Checker.
Select the Ledge Grab Checker game object, and create a new Tag name Ledge Grab Checker. In the Box Collider component, make sure to turn on Is Trigger to allow the Ledge Checker to enter into its collider. Last, add a Rigidbody component with its Use Gravity turned off.
Select the Ledge Checker object, turn on the Is Trigger setting under the Box Collider. Also, drag the Ledge script onto the Ledge Checker in the Inspector to create a Ledge script component.
Play the game and jump over to the Ledge Checker object. If the Ledge Grab Checker collides into the Ledge Checker, you will see the Player paused in the air as its Character Controller is now disabled.
In the Animator, delete the transition between Any State and Hanging Idle. Create a new transition from Running Jump to Hanging Idle. Create a Bool Parameter name Ledge Grab and click on the transition between Running Jump -> Hanging Idle. Turn off Has Exit Time, leave the Transition Duration setting for now, and set the Condition of Ledge Grab equals True.
Back in the Player script, in the GrabLedge() method we will add the anim variable to SetBool the LedgeGrab parameter to equal True.
Play the game and jump across to the Ledge Checker object. When the Ledge Grab Checker object collides with the Ledge Checker, we will see the Player hanging from the Ledge Checker.
Let’s turn off the Mesh Renderer of the Ledge Grab Checker and Ledge Checker objects to remove the appearance of the cubes onscreen.
Now we can see the Player hanging onto the Ledge Checker object even though its transparent.
Next time we will have the Player hang onto the ledge itself in its Hanging Idle animation, then climb up when pressing a special key.