There's more...

With this approach, the character will start to desynchronize with the ladder when the ladder is high enough. The animation error cumulates after each loop (and also when we transition to the ClimbIdle animation while climbing). I suggest three ways in which we can improve this feature:

  • One long animation instead of a loop: Instead of using a looped animation, we can prepare several long animations for different ladder lengths. Using the animator.speed variable, we can set the playback speed of the whole Animator component in runtime. If we set it to 0 while player is not holding the up arrow, it will pause the animation. Unfortunately, this approach pauses all the animations. We can get around this by creating a Blend Tree with our LongClimbLadder animation with Time Scale set to 0 in the first Motion Field and the same animation at full speed in the second Motion Field. Then you can create a parameter to control the blending between those two animations to stop the player on the ladder. You can also add a third Motion Field with the Time Scale set to -1 to make the character go down the ladder.
  • MatchTarget() solution: We can use the animator.MatchTarget() function to match the character's root node with our ladder's steps. Say our ladder has steps of height 30cm and our ClimbUp animation has roughly 30 cm root motion up. We can count the ClimbUp loops in runtime (with an Animation Event) and save them in a variable (let's name it numberOfLoops). Then we call MatchTarget() every time we play another loop of the ClimbUp animation. We set the targetPosition's Y component in the MatchTarget() function to be equal to numberOfLoops * 0.3f. That should synchronize our character with the ladder better.
  • Use IK: Finally, we can use IK for hands and feet to match the ladder's steps. The built-in void OnAnimatorIK(int layerIndex) function is a good solution. We have to use the animator.SetIKPosition() and animator.SetIKPositionWeight() functions to set goals for hands and feet. It is a good idea to create an Animation Curve in the ClimbUp animation and use its value in the animator.SetIKPositionWeight() function. You can find more about IK in the Using IK for interacting with scene objects recipe in Chapter 5, Character Actions and Expressions.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.37.126