How to do it...

To create a flying creature, follow these steps:

  1. Import the character with all required animations. Set the Idle animation Bake Into Pose options to true.
  2. Create a new Animator Controller.
  3. Create three parameters in the controller: Speed, DirectionHor, and DirectionVer.
  4. Create a new Blend Tree in the controller and double-click on it to enter its settings. Set its parameter to Speed.
  5. Add one Motion Field and one Blend Tree to the currently selected Blend Tree. Uncheck the Automate Thresholds option and set the first threshold to 0 and the second one to 1.
  6. Assign the Idle animation to the Motion Field.
  7. Click on the second Blend Tree (that connected to the first one). Set its type to 2D Freeform Cartesian.
  8. Set the first parameter (X) of the second Blend Tree to DirectionHor and the second one (Y) to DirectionVer.
  9. Add nine Motion Fields to the second Blend Tree.
  10. Set the Motion Fields as follows (also see the following screenshot):
    • FlyLeftDown: Pos X set to -45, Pos Y set to -45
    • FlyLeft: Pos X set to -45, Pos Y set to 0
    • FlyLeftUp: Pos X set to -45, Pos Y set to 45
    • FlyForwardDown: Pos X set to 0, Pos Y set to 45
    • FlyForward: Pos X set to 0, Pos Y set to 0
    • FlyForwardUp: Pos X set to 0, Pos Y set to 45
    • FlyRightDown: Pos X set to 45, Pos Y set to -45
    • FlyRight: Pos X set to 45, Pos Y set to 0
    • FlyRightUp: Pos X set to 45, Pos Y set to 45
Nested Blend Tree and a two dimensional Blend Tree used for flying
  1. Close the Animator Controller.
  2. Add the Rigidbody component to the character, freeze its rotations, and turn off its gravity.
  3. Add the Sphere Collider component to the character and make sure its Radius and Center properties are set correctly (it should encapsulate the whole character).
  4. Set the Animator component's Update Mode to Animate Physics and make sure the Apply Root Motion option is checked.
  5. Create a new script and call it FlyingPatrol.cs.
  6. In that script, we have two functions. The first function is PatrolCheck(), which checks the distance to the current patrol point (a game object in the scene) and assigns a new patrol point if our character is close enough to the current patrol point. All patrol points are stored in the public Transform[] patrolPoints array.
  1. In the second function, called CalculateDirectionsAndSpeed(), we first calculate the desiredMoveDirection vector. This is a vector starting at our character's transform.position and ending at currentPatrolPoint.position. We use this vector to calculate the speed value (it's the magnitude of this vector). After that we calculate the horizontalDirection vector by setting our desiredMoveDirection vector's Y component to 0. We use the horizontalDirection vector to calculate the horizontalAngle. We check the sign of the angle with the Vector3.Dot() and Mathf.Sign() functions, similar to what we did in the Using root motion to steer a character recipe. This is the angle between the horizontalDirection and our character's transform.forward vector. Next we calculate the verticalAngle. We use those calculated values to set the DirectionHor, DirectionVer, and Speed parameters in our Animator Controller. We use quite large dampTime values here to make the blends smoother:
       desiredMoveDirection = currentPatrolPoint.position -
            transform.position;
            speed = desiredMoveDirection.magnitude;
            horizontalDirection = new Vector3(desiredMoveDirection.x,
0f, desiredMoveDirection.z).normalized; horizontalAngle = Vector3.Angle(horizontalDirection, transform.forward) * Mathf.Sign( Vector3.Dot(horizontalDirection, transform.right)); verticalAngle = Vector3.Angle(desiredMoveDirection, horizontalDirection) * Mathf.Sign( Vector3.Dot(desiredMoveDirection, transform.up)); anim.SetFloat("Speed", speed, 0.25f, Time.deltaTime); anim.SetFloat("DirectionHor", horizontalAngle, 0.5f, Time.deltaTime); anim.SetFloat("DirectionVer", verticalAngle, 0.5f, Time.deltaTime);
  1. In the Update() function of our script, we call both the PatrolCheck() and CalculateDirectionsAndSpeed() functions.
  2. Assign the script to our character.
  3. Create several patrol points (empty game objects) and assign them to the Patrol Points array in our character's Flying Patrol script component.
..................Content has been hidden....................

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