- Unity 2017 Game AI Programming(Third Edition)
- Ray Barrera Aung Sithu Kyaw Thet Naing Swe
- 794字
- 2021-07-02 19:14:16
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.
- Clojure Programming Cookbook
- Microsoft Application Virtualization Cookbook
- 云原生Spring實戰
- 深入淺出Windows API程序設計:編程基礎篇
- Vue.js 3.0源碼解析(微課視頻版)
- Java開發入行真功夫
- Node.js Design Patterns
- Python Data Structures and Algorithms
- OpenCV with Python By Example
- IDA Pro權威指南(第2版)
- UML2面向對象分析與設計(第2版)
- 深入理解BootLoader
- 深入淺出Python數據分析
- Visual Basic語言程序設計基礎(第3版)
- IBM RUP參考與認證指南