How to do it...

To make a 180-degree turn, follow these steps:

  1. Create four transitions (see the following screenshot):
  2. Drag and drop your 180Turn animation into the Animator Controller to create a new state.
  3. Open the Animator Controller (this should be the same controller as in the previous recipe), add two float parameters, and call them DirectionRaw and SpeedRaw.
  4. Check the Bake Into Pose option for the Root Transform Position (Y) in the 180Turn animation.
  5. In the Animation Import Settings of the character, find your additional 180Turn animation. You may need to add it as a new clip by clicking on the plus button in the Clips section (Animations tab) and choosing the 180Turn animation as the source.
  6. Import the character and repeat all the steps from the Using root motion to steer a character and Using animations for better looking transitions recipes to have a character that is able to walk.
    • Idle | 180Turn with two conditions: The SpeedRaw parameter should be greater than 0.5 and the DirectionRaw parameter should be greater than 160. Has Exit Time should be set to false and Transition Duration should be set to around 0.1 seconds.
    • Idle | 180Turn (a second one) with two conditions: The SpeedRaw parameter should be greater than 0.5 and the DirectionRaw parameter should be less than -160. Has Exit Time should be set to false and Transition Duration set to around 0.1 seconds. We have two transitions between the same states because we want them to work as a logical OR operator; if any of them is met, the transition will be triggered.
    • 180Turn | Idle with one condition: Speed parameter less than 0.5. Has Exit Time should be set to true and Transition Duration set to around 0.1 seconds.
    • 180Turn | Steering with one condition: Speed parameter greater than 0.5. Has Exit Time should be set to true and Transition Duration set to around 0.1 seconds.
Animator Controller with additional 180Turn transition animation
  1. Edit the Idle | Steering transition by adding two more conditions: DirectionRaw parameter greater than -160 and DirectionRaw parameter less than 160. Multiple conditions in the same transition work as an AND logical operator; all of them have to be met to trigger the transition.
  2. The Animator Controller is set up, but we need to set the SpeedRaw and DirectionRaw parameters from scripts. We've created those two additional parameters because we need raw values here (not damped in time). First, open the script in which we calculate and set the direction and speed parameters. In this example, it is called RootMotionSteering.cs; you can find it in the Chapter 04 Character movementRecipe 03 Using root motion to steer a characterScripts directory.
  3. We have two float variables there: direction and speed. They are being calculated every frame. We just need to make them public to be able to read them in another script (they are already public in the example script).
  1. Write another script and call it SetRawDirectionAndSpeed.cs (you can find it in the Scripts directory of this recipe). To set the DirectionRaw and SpeedRaw parameters, we use the following lines in the Update() function:
        anim.SetFloat("DirectionRaw", steeringScript.direction); 
        anim.SetFloat("SpeedRaw", steeringScript.speed); 
  1. In the preceding lines, the anim variable is the reference to the Animator component, the steeringScript variable is the reference to the RootMotionScript component (we set it with the steeringScript = GetComponent<RootMotionSteering>() line in the Start() function), and the steeringScript.direction and steeringScript.speed variables are the calculated raw direction and speed values.
Alternatively, you can call the anim.SetFloat("DirectionRaw", direction) and anim.SetFloat("SpeedRaw", speed) methods directly in the Update() function of the RootMotionSteering.cs script (without writing another script).
  1. Assign the script to the character, play the game, and try to walk in the direction opposite to the direction the character is facing. The character should start with an 180Turn animation first.
..................Content has been hidden....................

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