官术网_书友最值得收藏!

Making the cogs turn

This is the part I'm sure you've been waiting for. I know, I've kept you waiting, but for good reason. As we now get ready to pe into the coding, we do so with a good understanding of the logical connection between the states in our FSM. Without further ado, select our Patrol state. In the hierarchy, you'll see a button labeled Add Behaviour. Clicking this gives you a context menu very similar to the Add Component button on regular game objects, but as we mentioned before, this button creates the oh-so-unique state machine behaviors.

Go ahead and name this behavior TankPatrolState. Doing so creates a script of the same name in your project and attaches it to the state we created it from. You can open this script via the project window, or by double-clicking on the name of the script in the inspector. What you'll find inside will look similar to this:

using UnityEngine; 
using System.Collections; 
 
public class TankPatrolState : StateMachineBehaviour { 
 
   // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state 
   //override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { 
   // 
   //} 
 
   // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks 
   //override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { 
   // 
   //} 
 
   // OnStateExit is called when a transition ends and the state machine finishes evaluating this state 
   //override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { 
   // 
   //} 
 
   // OnStateMove is called right after Animator.OnAnimatorMove(). Code that processes and affects root motion should be implemented here 
   //override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { 
   // 
   //} 
 
   // OnStateIK is called right after Animator.OnAnimatorIK(). Code that sets up animation IK (inverse kinematics) should be implemented here. 
   //override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { 
   // 
   //} 
}

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Before we begin, uncomment each method. Let's break it down step by step. Unity creates this file for you, but all the methods are commented out. Essentially, the commented code acts as a guide. Much like the methods provided for you in a MonoBehaviour, these methods are called for you by the underlying logic. You don't need to know what's going on behind the scenes to use them; you simply have to know when they are called to leverage them. Luckily, the commented code provides a brief description of when each method is called, and the names are fairly descriptive themselves. There are two methods here we don't need to worry about, OnStateIK and OnStateMove, which are animation messages, so go ahead and delete them and save the file.

To reiterate what's stated in the code's comments, the following things happen:

  • OnStateEnter is called when you enter the state, as soon as the
    transition starts
  • OnStateUpdate is called on each frame, after the MonoBehaviors update
  • OnStateExit is called after the transition out of the state is finished

The following two states, as we mentioned, are animation-specific, so we do not use those for our purposes:

  • OnStateIK is called just before the IK system gets updated; this is an animation and rig-specific concept
  • OnStateMove is used on avatars that are set up to use root motion

Another important piece of information to note is the parameters passed into these methods:

  • The animator parameter is a reference to the animator that contains this animator controller, and therefore this state machine. By extension, you can fetch a reference to the game object that the animator controller is on, and from there, you can grab any other components attached to it. Remember, the state machine behavior exists only as an asset, and does not exist in the class, meaning this is the best way to get references to runtime classes, such as mono behaviors.
  • The animator state info provides information about the state you're currently in; however, the uses for this are primarily focused on animation state information, so it's not as useful for our application.
  • Lastly, we have the layer index, which is an integer telling us which layer within the state machine our state is in. The base layer is index zero, and each layer above that is a higher number.

Now that we understand the basics of state machine behavior, let's get the rest of our components in order. Before we can actually see these behaviors in action, we have to go back to our state machine and add some parameters that will drive the states.

主站蜘蛛池模板: 合川市| 松溪县| 绥江县| 宾阳县| 乌鲁木齐市| 海盐县| 清水县| 堆龙德庆县| 喜德县| 买车| 娄底市| 洛川县| 资溪县| 马关县| 大同县| 呼玛县| 开鲁县| 舟山市| 天柱县| 雅江县| 柘城县| 吴江市| 金溪县| 南京市| 古交市| 营口市| 松阳县| 白玉县| 怀集县| 吉首市| 香河县| 延津县| 阜康市| 永平县| 东乡| 珠海市| 西乌| 安图县| 万宁市| 襄城县| 柞水县|