• No results found

Using triggers to grab an edge while jumping

In document Unity 5.x Animation Cookbook (Page 152-157)

agent.velocity = anim.deltaPosition / Time.deltaTime;

transform.rotation = anim.rootRotation;

}

Using triggers to grab an edge while jumping

Grabbing an edge while jumping is a common feature, especially in platform games. It is easily done in Unity with a small amount of scripting. This recipe covers a simple edge grab functionality.

Getting ready

You need three new animations for our character: EdgeGrab, EdgeGrabLoop, and EdgeGrabClimb. The first one is a transition from the InAir animation to

the EdgeGrabLoop animation. It should have minimum movement in the root node if possible. The second one is a looped animation of hanging on the cliff's edge. The last one is an animation that uses root motion to climb the cliff's edge and ends with an Idle pose. See the following screenshot for reference:

Frames of the EdgeGrabClimb animation using root motion to climb over the cliff's edge

EdgeGrab and EdgeGrabLoop animations should have all Bake Into Pose options selected in Import Settings. EdgeGrabLoop should also have the Loop Time option selected. You can also use the provided example Unity project and go to the Chapter 04 Character movement\Recipe 08 Using triggers to grab an edge while jumping directory.

You will find an Example.unity scene there. Open it, play the game, and press the space bar to jump towards the edge. After the character grabs it, press the up arrow to climb over the edge.

How to do it…

To be able to grab a cliff's edge while jumping, follow these steps:

Import your character to Unity. Make sure your character can move and jump 1. (follow the Making a character jump with 3-phase animation recipe if needed). The

character should use a Rigidbody component for moving and jumping. You can still use root motion to move and steer the character.

Place the character in your scene and make sure it has the Player tag set.

2.

You need to have a cliff to be able to grab its edge. Import a suitable model or use 3. a Cube from the Game Object | 3D Object | Cube menu. The cliff game object

should have a collider (Mesh Collider for example) because we use a Rigid Body character.

Add a trigger game object (an empty game object with a Box Collider component 4. set to Is Trigger). Name it GrabTrigger for clarity. Place the trigger near the

edge of the cliff. The trigger is marked with the number 1, as shown in the screenshot after step 5.

Add another empty game object and name it RootTarget. Place it in the position 5. where your character's transform should be when it grabs the edge. It is marked

with the number 2, as shown in the following screenshot. You can adjust the exact position later.

GrabTrigger and RootTarget game objects' placement

Open the character's Animator Controller. Drag and drop

6. your GrabEdge, GrabEdgeLoop, and GrabEdgeClimb animations into the controller.

Add a Trigger GrabEdge parameter and a Trigger PullUp parameter to the 7. controller.

Create these transitions:

8. Jump | GrabEdge with one condition: GrabEdge trigger parameter. Has Exit Time should be set to false and Transition Duration set to around 0.2 seconds.

InAir | GrabEdge with one condition: GrabEdge trigger parameter. Has Exit Time should be set to false and Transition Duration set to around 0.2 seconds.

GrabEdge | GrabEdgeLoop with no conditions. Has Exit Time should be set to true and Transition Duration set to around 0.2 seconds.

GrabEdgeLoop | GrabEdgeClimbing with one condition: PullUp trigger parameter. Has Exit Time should be set to false and Transition Duration set to around 0.2 seconds.

GrabEdgeClimbing | Idle with no conditions: Has Exit Time should be set to true and Transition Duration set to around 0.2 seconds.

Close the Animator Controller and assign it to the character's Animator 9. component.

Create a new script and call it EdgeGrab.cs (you can find the finished script in 10. the Scripts directory of this recipe). The goal of this script is to play appropriate

animation when we touch the edge of the cliff. We also need to disable collisions and physics and make sure our character will be in the right place while playing the animation (in the RootTarget game object's position).

First we need a Grab() function to handle the situation when player starts 11. touching the edge (we will call this function from our GrabTrigger game object

later):

This function has a target parameter which will hold the reference to 12. the RootTarget game object. We assign this parameter to a class member

variable grabTarget (we will use it later). We are starting to play the GrabEdge animation by setting the GrabEdge Trigger in the controller. We are also setting a flag (class member) adjustPosition to true. This flag is used later to

determine whether we should match our character's position with the RootTarget game object's position. Then we disable two scripts stored in steeringScript and jumpScript variables. We are doing it only because we don't want them to interfere with our edge grab action. We are also setting the isKinematic option on our Rigidbody component (the rb variable stores the reference to it) to disable collisions and gravity. Lastly, we enable root motion for our Animator

Component (reference to which is stored in the anim variable). That makes the GrabEdgeClimb animation work.

Next we have to write the GrabLerp() function that handles the character's 13. position and RootTarget's position matching:

void GrabLerp() {

if (grabTarget == null || !adjustPosition) {

return;

}

if ((transform.position grabTarget.

In document Unity 5.x Animation Cookbook (Page 152-157)