Creating CrossFade animation

In this section, we will apply and create the animation clip to our character and make it suitable for each action such as idle, walk, run, jump, and fall animation.

Engage Thrusters

We will start with creating the Awake() function:

  1. Go to the Awake() function to set up warpMode of the animation, and type the following highlighted script:
    //Using Awake to set up parameters before Initialize
    public function Awake() : void {
      controller = GetComponent(CharacterController);
      b_isRun = false;
      b_isBackward = false;
      b_isJumping = false;
      f_moveSpeed = speed;
      c_collisionFlags = CollisionFlags.CollidedBelow;
      
      //Set warpMode for each animation clip
      animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;
      animation[fallPoseAnimation.name].wrapMode = WrapMode.ClampForever;
      animation[idleAnimation.name].wrapMode = WrapMode.Loop;
      animation[runAnimation.name].wrapMode = WrapMode.Loop;
      animation[walkAnimation.name].wrapMode = WrapMode.Loop;
    }
  2. Go back to the Update() function in the CharacterControl.js file between c_collisionFlags = controller.Move(v3_movement); and if (v3_moveDirection != Vector3.zero) near the bottom line of this function; add the following highlighted script:
    // Move the controller
       c_collisionFlags = controller.Move(v3_movement);
    
      //Play animation
       if (b_isJumping) {
        if (controller.velocity.y > 0 ) {
          animation[jumpPoseAnimation.name].speed = jumpAnimationSpeed;
          animation.CrossFade(jumpPoseAnimation.name, 0.1);
        } else {
          animation[fallPoseAnimation.name].speed = fallAnimationSpeed;
          animation.CrossFade(fallPoseAnimation.name, 0.1);
        }
      } else {
        if (IsAir()) { // Fall down
          animation[fallPoseAnimation.name].speed = fallAnimationSpeed;
          animation.CrossFade(fallPoseAnimation.name, 0.1);
        } else { //Not fall down
           //If the character has no velocity or very close to 0 show idle animation
          if(controller.velocity.sqrMagnitude < 0.1) {
            animation[idleAnimation.name].speed = idleAnimationSpeed;
            animation.CrossFade(idleAnimation.name, 0.1);
          } else { //Checking if the character walks or runs
            if (b_isRun) {
              animation[runAnimation.name].speed = runAnimationSpeed;
              animation.CrossFade(runAnimation.name, 0.1);
            } else {
              animation[walkAnimation.name].speed = walkAnimationSpeed;
              animation.CrossFade(walkAnimation.name, 0.1);
            }
          }
        }
      }
       //Update rotation of the character
        if (v3_moveDirection != Vector3.zero) {
          transform.rotation = Quaternion.LookRotation(v3_moveDirection);
        }
    }

    We just added the script to check what the animation clip should play when the character is in each action. In the first section, we are checking if we are jumping. If we are, we play also, Jump and not jump. If we are falling down, we play the Fall and not fall. Then, if we are moving by walking, we play Walk Animation. If we are running, we play Run Animation. If we are not doing anything, we play Idle Animation.

  3. Finally, we go back to Unity and add the animation clip to our character by clicking on the Heroine_animate object in the Hierarchy View to bring up its Inspector view, under the Character Control component in the Inspector view, and set the following:
    • Idle Animation: idle
    • Walk Animation: walk
    • Run Animation: run
    • Jump Animation: jump
    • Fall Animation: fall

    We will see the result of the Inspector view, as shown inthe following screenshot:

    Engage Thrusters

    Note

    We can find all the animation clips by going to the Project view in Chapter4 | FBX | Heroine_animate, as shown in the following screenshot.

    Engage Thrusters

Objective Complete - Mini Debriefing

We just added a new script to check what the animation clip should play when the character is in each action. We also set the speed of each animation by using animation[name].speed, set the warpMode by using animation[name].warpMode, and we used the animation.CrossFade(name, time) to blend one animation clip to another.

Classified Intel

In this step, we set the speed of our animation by using animation[name].speed, where animation[name] is the animation clip that we have already set up in the first step.

The speed parameter is basically the speed with which the animation clip is played. For example, if we set our animation from 3D Software to play this animation in one second, and we set the speed of this animation equal to 1, then the animation clip will play at the same speed as the source animation. On the other hand, if we set up the speed to 2, this animation clip will play twice as fast as our source animation. Also, if we set the number lower than 1, the animation will play that many times slower than the source.

The animation.CrossFade() function will cross fade from the current animation clip to another animation, and we pass its name to this function. We can also control how much time we want to cross fade for by setting the number of times. More details on this function are available at: http://unity3d.com/support/documentation/ScriptReference/Animation.CrossFade.html.

..................Content has been hidden....................

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