ACCESS the FULL COURSE here: https://academy.zenva.com/product/rpg...
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, will execute whatever is in the chase method, right? So if we do that, we have OnEnter, and then while in this state, we have something to execute, and then we want something to execute whenever we leave the state, so for this case, again, I would want to stop the animation once I leave the chase state, so you trigger it when you enter, and you trigger it when you leave it. Pretty simple, and that will be the same setup we have for all of our states, and I'm saying the word state a lot, here, and we're about to write the state class, so what is a state going to be? Well, it's going to be the method that we call, every single frame, so like I said, we have the chase state, idle state and attack state or whatever I said, chase state, every single frame, we're going to call the chase code, right? The follow the player code, whatever that might be; we're going to use a navmesh, with a nav agent, and just pathfind to the player, you know, make it kinda cool that way, so it's gonna be the thing that happens every single frame that we're checking for, every single frame, and then a state also has to have the OnEnter method and the OnExit method I just explained, and it also has to have the ability to execute that, and we'll write all of that in our state class.
Watch video How to Set up a State Class in Unity online without registration, duration hours minute second in high quality. This video was added by user Zenva 19 August 2018, don't forget to share it with your friends and acquaintances, it has been viewed on our site 1,587 once and liked it 9 people.