How to do it...

To use the Blend Trees methods for aiming, follow these steps:

  1. Import your character with all the necessary animations to Unity and place it on the Scene.
  2. Follow the Using Avatar Masks and animator controller layers to walk and aim recipe to make the character walk and aim.
  3. Open the controller and go to the Aim Layer.
  4. Delete the AimForward state and create a new Blend Tree.
  5. Create two float parameters: AimVer and AimHor.
  1. Set the Blend Type to Freeform Cartesian.
  2. Set the first parameter of the Blend Tree to AimHor and the second one to AimVer.
  3. Create nine Motion Fields in the Blend Tree and assign all the aim animations to them.
  4. Set the Motion Fields as follows (also see the following screenshot for reference):
    • AimLeftDownPos X set to -45, Pos Y set to -45
    • AimLeftPos X set to -45, Pos Y set to 0
    • AimLeftUpPos X set to -45, Pos Y set to 45
    • AimForwardDownPos X set to 0, Pos Y set to -45
    • AimForwardPos X set to 0, Pos Y set to 0
    • AimForwardUpPos X set to 0, Pos Y set to 45
    • AimRightDownPos X set to 45, Pos Y set to -45
    • AimRightPos X set to 45, Pos Y set to 0
    • AimRightUpPos X set to 45, Pos Y set to 45
Aim Blend Tree settings
  1. Create a new Sphere object and name it AimTarget.
  2. Make AimTarget a child of the Main Camera game object. We are using the same CameraLook.cs script in this recipe. You can find the script in the Shared Scripts folder in the provided example Unity project.
  3. Offset the AimTarget position in its local Z axis by around 20 units.
  4. Create a new script and call it AimWithBlendTree.cs. In the Update() function of this script, we first calculate the aimVector. This is a vector from a helper aimNode transform to the target transform. Then we use this vector to calculate a horAimVector, a horizontal vector for aiming. Then we use the horAimVector to calculate the aimHor angle. We use the Vector3.Angle() method to find the angle between the horAimVector and transform.forward axis and Mathf.Sign() with Vector3.Dot() to determine the sign of the angle. We calculate the aimVer angle in a similar way, but we use the horAimVector instead of the transform.forward axis. Lastly, we assign the AimVer and AimHor parameters in the controller:
        aimVector = target.position - aimNode.position; 
 
        horAimVector = aimVector; 
        horAimVector.y = 0f; 
 
        aimHor = Vector3.Angle(horAimVector, transform.forward) *         Mathf.Sign(Vector3.Dot(horAimVector, transform.right)); 
 
        aimVer = Vector3.Angle(horAimVector, aimVector) * 
        Mathf.Sign(Vector3.Dot(aimVector, Vector3.up)); 
 
        anim.SetFloat("AimHor", aimHor, aimSmooth, Time.deltaTime); 
        anim.SetFloat("AimVer", aimVer, aimSmooth, Time.deltaTime); 
  1. Assign the script to the character. Drag and drop the AimTarget transform to the Target field of the script.
  2. Create an empty game object and name it AimNode. Place it roughly where the chest of the character is and make it a child of the character's transform.
  3. Assign the AimNode transform to the Aim Node field of the script. You can use the AimNode to adjust the aimVector calculation.
  4. Play the game to see the effect.
..................Content has been hidden....................

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