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

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.

主站蜘蛛池模板: 和田市| 芮城县| 宁晋县| 安阳市| 泸水县| 门头沟区| 天全县| 永平县| 三原县| 和田县| 资中县| 库尔勒市| 东乡族自治县| 卫辉市| 桐柏县| 牙克石市| 南康市| 温州市| 特克斯县| 封开县| 增城市| 伊春市| 申扎县| 七台河市| 偏关县| 师宗县| 丁青县| 新宁县| 乐平市| 北京市| 沈阳市| 贺兰县| 景德镇市| 东乡| 聂荣县| 南安市| 陆川县| 图木舒克市| 长宁区| 高青县| 绵阳市|