How To Use Bolt State Machines In Unity - Visual Scripting

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so hello welcome back to another unity tutorial now that unity have made bolt one free in the asset store and they've confirmed that bolt two will be free i decided to do some more videos on it in this video i'll be showing you how to use the bolt state machine i hope you're looking forward to it let's get started okay so here we are in unity now i've kind of shifted around my window so that we have a place here for me to work in the graph and so we have the game animator and scene view up here okay so what do we have for this demo well we've got the player the player simply has an animator that plays an idol animation that's all it does right now it's just something to put on the screen okay we're going to be writing a state machine that does stuff with the player depending on which state it's in if you haven't got bolt installed or you don't know how to check out my previous video on bolt i actually showed how to set it up that was just before they actually announced that it's going to be free so at the time i didn't know that it would be free and that you know loads of people would want to probably have me do tutorials on it and jump on board so now it's free you can go ahead to the asset store you can add it to your assets and then come and download it in unity by going to the window package manager and if you go to my assets once you've added it to your assets it'll show up on here in a second it's just obviously loading okay and we have bolt here you can import it and as i said in my previous video on bolt and visual scripting i actually showed you how to install and set it up so i don't need to do that again in this video okay let's get into it so a state machine okay i'll assume you know what state machine is but just in case you don't if you actually look at the animator which i'll assume you used before okay you have different states so we have the entry state which isn't really a state it just is basically when you start go to this state right so this is really the starting state here and i can have other states over different animations and i can have these transitions these arrows that go between the states and then on the transition i can say you know what causes it to transition but with the built-in animator you're limited to just having uh so i don't mean to add a new layer sorry let's delete that if you go to parameters you can add different parameters floats in spools and triggers okay and then you need to write your own script that changed those values to actually drive the animator which is okay for some animation but what you actually want to do most the time when you've got a state machine is not have any of this this is kind of designed more towards animation but a lot of state is not animation related and even if you are doing it for your player character that has animation you can still use both of these i'd use bolt for doing the logic and then simply have bolt tell the animator to change those values for the animation it'll make more sense the more you use it we're not doing an animation example this time anyway let's go over to our player okay so last time i showed you bolt we made a machine okay a flow machine that's for having nodes so if we go over here and press new okay and let's just for now just chuck it in tutorials bolt state machine macros and call it test okay this will now make us a basically a script right we have start and update and you can do whatever you want in here okay and this is just like a mono behavior in the sense that it's on it's like a component on here and it runs but with state machines you want different states that run at different times so instead of this we'll remove the flow machine and i'll actually delete this this test machine i just made we're actually going to instead use the state machine okay and i'll do the same thing where i make a new state machine and i go chuck it in uh here here and we'll call it like player state machine okay so i've made this for the player and by default it gives us one state okay and this state is where it starts if i made a second state by right clicking and clicking flow state i can then make this the starting state by toggling start okay i'm not actually sure what happens if you have multiple as your start state i guess i'd advise against doing that because you know you don't really want that in a state machine um and right now there is no starting state so let's put this back on okay so what it means is when i start okay this is going to um happen all the stuff in here which is currently just the enter callback the exit callback and the update okay so obviously when i press play both of these are going to start happening this will be fired whoops sorry this will be fired once and the update will be fired every frame but it's only every frame that this state is active so if we have multiple states you can have logic in those states that you only want to run whenever and then you can have transitions so just like how i showed you in the animator we have these transitions okay we're going to set some up here so let's start so to make our state machine let's start off with an idle state that does nothing it's just where we start so we're going to make a new flow state open it up and i'll make this full screen okay let's call it idle and we don't want anything so let's just get rid of this go back from full screen and if you are still inside here you can go back to the route by clicking where it says player and has these icons okay you can click this and here we have our idle state and we start here and nothing ever happens because it's empty okay so let's make one of our other states now if you're building a full character you might have jumping and swimming and all these different states or with your ui you might have different menu pages but for this we're going to have a state called invisible okay so what we'll do is we'll make the invisible state by going over here and we'll call it invis evolve and for invisible we don't want to do every anything every frame at least for this example we want to do something when we enter and when we exit okay so when we enter we want the player to go invisible and when they exit to come out with invisibility and then all we need to do is tie these two states together so that we can go from idle to invisible and from invisible back to idle on different conditions and those conditions can be whatever you like so how this works is i can right click on idle and make a transition to invisible and what it'll do is it'll make this thing in the middle which is the transition which you can then double click and go into and this is where you write your logic for when you should transition and by calling the trigger transition method it actually will go from this state to this state and in this case we also want to be able to go back so let's make a transition back to idle and now we have two and obviously the arrows show you which direction so this top one is the condition to go from idle to invisible and the bottom one is to go back so let's first get the transition set up so we can actually watch it in the editor we can we can watch the graph while we're playing to see it going from idle to invisible and back and then once that's done we can actually have the logic invisible so what i want to do is we want to say to go from idle to invisible into here what do we actually want to do this is completely down to you whatever it is that makes your state change you put in here this is the logic for it i'm going to really simply just make it on a key press so if we go to events input and then keyboard input let's say let's leave it like this when the space key is pressed down trigger the transition simple now obviously this is as basic as it can be you might want it to be you know checking the player's health when the health goes below a certain value then trigger the transition or whatever right it's up to you you just end up calling this whenever you want the transition to happen so when i press the space key and obviously this is a one-way thing so when i'm in the invisible state it won't actually check for that to take me back and you can watch this in action so if we press play now and give it a second it'll actually show us that the idle state is active okay it just does a refresh idle is active when i hit space now it goes across to invisible you actually see the little animation for that but when i press space again it doesn't go back and if i quickly reset this and press play again i can go into the little keyboard transition here and what i can do is i can actually watch this so when i hit space this is going to happen it's going to go across here showing that space happened but when i press it now it doesn't actually show anymore so it's not even checking for it which is really nice it only does the thing when it's in the state to do so and we want a way to go back so i'll go into here make it full screen and let's do some different inputs so let's say in the input on mouse input so when i press the left mouse button down let's trigger the transition okay like so and then we go back and here you see we have the keyboard event to go to invisible and the mouse event to come back by pressing play we can now watch the full changing of states okay so we start off in idle give it a second and we hit space we go to invisible and we left click and we go back if i click left click now nothing happens so i go space left click space locally and so on and so forth and now we just want something to happen in invisible because when i press these keys now you'll actually notice if i hit left click this goes hit space this goes left click this goes but nothing is actually coming out here so now we can write our logic for when we enter the invisible state so obviously for going invisible you might you know write your own invisibility shader of some kind but to keep this simple i'm just going to disable the mesh renderers okay so i've got the two skin mesh renderers one for the joints and one for the surface so we need to be able to access these in our code and we could just do the you know get components method which will grab them fresh every single time but we can just cache them it's a lot easier so on the player for the variables let's add a variable called renderers like so and then we can set the type to be renderer and we don't just want a renderer we actually want a list of renderers so down here list of renderer and then we can add two and we want the joints and the surface okay and now by doing this if we go back into our graph and we go full screen we go to invisible state on our object these are the variables we have access to we have access to well it's just one variable which is the list of renderers and i can drag this in and here are our renderers okay this is our renderer or our list of renderers so what do we want to do we want to loop over them so let's make a for each okay so we can say for each loop whoops i clicked off it for each loop so obviously you pass in the list to this little thing here and then you call the for each when we enter the state and these different things here so exit means you know what happens after the loop has been done body is every single iteration of the loop index is obviously how many how how many times you've done the loop and then item is the actual element in that loop so for looping over all of the different renderers and wanting to disable them what we can do is we go here and we can say renderer dot enabled because we're wanting to set this to be false right so we see renderer.enabled set and the renderer is the item we want to set it to be false and we call it every time here there we go and that's it and then we want to replicate that on exit state so let's grab these free and copy paste them over here okay we call it here and the only thing we do is when we exit we actually enable the mesh renderers instead okay and now if we go out full screen and go back here and press play let's watch it in action so we're in idle and i hit space and we go invisible and i hit left click and we come back let's enter the state and actually watch it happen okay let's press overview to get it to shape like this so if i hit space now you actually watch this gets called it grabs the renderers it does a for each loop and for each item in the loop we set the renderer to be disabled and then when we exit when i left click okay it exits the state it does a freeze loop with the renderers and instead it actually just sets them to be enabled instead so yeah that's it for this video i hope you enjoyed if you did please leave a like and subscribe let me know down below what you want to see next i hope you're enjoying bolt as much as i am thanks always for watching i'll see you in the next one and goodbye but of course before i go i've got to thank my patrons special thanks to taylor rustio francisco lira liz kimber beardedi benjamin hilder david mcdermott dustin miller jake nixon john selig euro sleta ln matt fryer renee san marcos fabian renault malvin and rack if you'd like to help support the channel monetarily link to my patreon is down below if not then it would be greatly appreciated if you could check out our social media links down below go follow us on twitch twitter join our discord and check out our course on udemy thanks again for watching and i'll see you in the next one
Info
Channel: Dapper Dino
Views: 19,775
Rating: undefined out of 5
Keywords: Get start with Unity, Get started with game development, How to make a game, What is a game engine, Unity tutorials, Game design, Game developer, Game designer, Video game developer, How to make games, Game development software, C# Game development, How to, Dapper dino, What is Unity, Animation, Animator, C#, Coding, Programming, Multiplayer, Train, Shader, Material, Shader graph, Effect, Visual, Particles, AI, ML, Scripting, Visual scripting, Graph, State machine, Flow graph, Flow machine
Id: SVpkh3kMIcg
Channel Id: undefined
Length: 12min 3sec (723 seconds)
Published: Thu Jul 30 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.