How to Set up a State Class in Unity
Zenva
ACCESS the FULL COURSE here: https://academy.zenva.com/product/rpg-academy/?zva_src=youtube-rpg-academy
TRANSCRIPT
Hey, guys, welcome back; in the first lesson of the AI course, we're going to set up our state state class; that's going to allow us to define what an actual state is in our state machine. To get started, we're gonna open up the included project, go to Open, here, and I know I have it on my desktop, so I'm just gonna go to Zenva AI, Select folder, and I'm gonna open up the latest released version, with just a Windows build target's fine for now, and go ahead and open that up. Included in the project, is a simple, little tutorial scene that has a couple of objects in the scene, just to give us something to start with, but we're not gonna worry about the level design for now; what we want to do, first of all, is just create a scripts folder, so we can keep all of our scripts in one location, so go to Create-Folder, I'm gonna create one called Scripts, here, and open that up. Now, in here, like I said, we're gonna create the state class first, so I'm going to go to Create-C# Script, and I want to call it State, so before we start writing this, I kind of want to explain the process that we're going to have to use for our state machine to actually work; so, the basis for our state machine is going to be jumping between methods, right? Let's say we have three methods; one method is an idle method, one method is an attack method, and one method is a chase method, and these methods are going to act as, let's say they're going to be processed every frame, so take update, for an example, in Unity, it happens every single frame; anything that happens in there, will happen every single frame, right? So we want our states to also execute every single frame, so we'll keep that in mind, as we're writing this, that way, if we say go from a to b, it won't have to go from a to b in one frame. We can do it over time, and handle that very easily; another thing that's gonna be important, is the ability to transition to and from a state, and call, what's called an OnEnter and an OnExit method, right? So whenever we jump into that state, I want to trigger something, so let's say we jump into the chase stay, I'm chasing the player; what do I want to happen? Well, instead of just chasing, I also want to trigger animation at the start of that, so we'll say, zombie, when it starts chasing the player; right when it goes into that state, we're going to fire off the OnEnter method that we're going to set up for chase, and it's going to trigger the animation event, or maybe just make a noise, or something, whatever it might be that you want to happen at the beginning of the state, and then we're going to process the state, so every single frame while in that state, ... https://www.youtube.com/watch?v=jp4VginglkI
19893057 Bytes