How To Make A State Machine Character Controller - Advanced Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so hello welcome back to another unity tutorial in this video i'll be showing you how we can use bolt state machine to help us build a character controller it currently has support for walking jumping falling and sliding and i'll be sure to add support for climbing and other actions in the future i hope you're looking forward to it let's get started but first i'd like to thank admix for sponsoring this video admix is a platform designed to help devs monetize their game without interrupting the player's experience by seamlessly placing ads inside the game world it takes less than one hour to get set up and with no coding required just drag and drop the ad placements into your game it's also fully integrated with unity and unreal engine there's also an online dashboard with plenty of analytics to help you optimize as you go check it out by following a link in description down below so here we are inside unity and i've hit play i can move my character around and as you see on the right we're in the locomotion state because it's blue and if i was to jump and leave the ground we go into the falling state and then when we hit the ground we go back to locomotion and then if i was to go up any of these ramps it stays in locomotion because they're not too steep they're you know they're still within my limit of being able to walk up and down them but as soon as i step over the edge here it actually becomes a bit too steep so if you if you notice that it transitions from locomotion into sliding and then when i reach the end i go into falling because there's nothing beneath me anymore and then when i hit the ground it goes back into locomotion and you can imagine how i could quite easily hook animation up to this and i'll be able to have a walking animation and then on here a sliding and then a falling and maybe a landed animation i also have this other big slope here for testing so i can't try and jump up this slope i can only jump when i'm on the ground and then when i actually start sliding down it turns around to face you know going down and i just stops me jumping up so i can't try and get up that slope and i've also got the steps here it supports steps and i understand steps are built into the character controller it has something to deal with steps but it's not that simple when you start adding other stuff like this when i added the falling state originally if i walked down the stairs it would transition between falling and sliding and locomotion as i walk down the stairs but i've sorted that out now and same with the slope over here usually what would happen is if i tried to walk down the slope i'd leave the slope then fall down to it then leave the slope and fall down to it but i actually stick to the slope as i walk up and down some of you might be wondering you know why not just use unity's built-in animator for this but i find that bolt state machine is a lot better because you don't have to actually make new scripts for all these different uh transitions so normally the bolt animator is limited to just transitioning off triggers and floats and balls but with with vault you can actually write the conditions in here as code inside the transitions you can also hook up custom logic inside states when you enter and exit them which you can also do inside the normal unity animator but you have to make an entirely new script to do it and i imagine that if your game has let's say 50 different enemies or 100 different enemies and maybe they all work slightly differently it'd be a pain to create a hundred different scripts one per enemy so i think it's a lot better to have each thing so the player or the enemies just have a state machine graph that has their custom logic for transitioning and now let's look into how it actually works so as i said i've already done a character controller video before and it works a little bit like this one does let's have a look at the player so we've got the input binder like i'll show you some of these later but the important stuff here is the character motor this is what actually moves the player and we're only meant to move the player once per frame so what i do is i store a list of movement modifiers and down here whenever we move i just loop over them all and i grab their value and this value is just a vector free and it's how much should this thing move per frame and in a particular frame let's say when we're in the locomotion state we have this walking and jumping enabled notice how sliding is disabled and walking movement if we look over here it just returns our input times our move speed so that's what gives us the normal you know walking around movement and then if we were to look into jumping this handles checking if we're grounded and if we hit the spacebar key to jump and if we jump and we're on the ground then do all this stuff and it also handles holding down space to jump higher so we have all that logic in here and this is also a movement modifier so when i jump i can still move mid-air and that's because the walking and the jumping scripts are both enabled so what happens is i'm holding forward so it tries to move it forwards and because i'm jumping it moves me up or down when i'm falling so both these are on by default and then the condition to go between the two is if i'm not grounded then go to falling and then from falling if i am grounded go back to locomotion and then in here is when i can add in animations so i could get reference to the animator on here when i when i add animation and i can say play the falling animation or the jumping or the you know landed animation when i go from falling to locomotion it means i've landed on the ground so i might want some landing animation now sliding was a lot more complex it took me ages to get it to work the actual logic here isn't more complex it's just saying should we slide and i'll show you that in a minute but yeah should we slide if we should slide if that's true then turn off walking turn off jumping and transition and then it's the reverse coming back so if we look over here it's saying uh should we slide if we shouldn't slide and then it checks whether we're grounded or not so if we should stop sliding and we're not on the ground then we go to falling if we stop sliding and we are grounded then we go back to locomotion and that helps this whole flow work correctly so when we're when we're sliding the sliding movement is on but these other two walking and jumping are off so it's up to this script to handle all of the sliding movement and the ball for should slide has some logic code say well if we're on the ground and do a raycast and figure out the normal vector of the ground uh if for example the raycast doesn't hit because maybe the direct bottom of our character is over a ledge then we just use the normal of the previous bit of ground we touched if we look over here the motor stores the ground normal whenever it hits the ground and this code is just various stuff i've come up with over time and i've found on online forums and stuff and i've kind of mashed it all together and fixed bugs and tweaked it and made it work with the state machine to where it all works how it does right now and i found that just having the separate types of movement in their own script and just turning them on and off is actually quite an effective way of doing this because it means that i can then just make an enemy that let's say doesn't have jumping or doesn't slide or doesn't walk for some reason and i can just remove those particular scripts and you know in the state machines i just don't reference them and it works just fine as opposed to what i've had to do in the past which is make like separate scripts per enemy the thing with these separate scripts is that they're reusable for anything that jumps i just stick that on it and in the enemy ai scripts or whatever i just called jump and by ai scripts i mean i can actually use the enemy state machines to handle this so i can say for the enemy state machine um you know check if the player's in range and if so then whatever switch to the attack state when i make that but for now just for the movement uh you know jumping is pretty simple walking is the simplest it's just literally a vector times by a float um i said jumping's a bit more complex because i've got um the ability to hold to jump for longer and sliding is a bit more complex because it does ray casts and then figures out the ground normal and you know it's not that bad this is whenever we say like movement modifier dot value this is what happens for the sliding script and if you remember over in the motor the motor stores all the movement modifiers it just says loop over them and add on their value so when sliding is the only one that's enabled it's just the sliding movement but then when walking and jumping are enabled it's both of those added together and it allows me to just let's say make you know a different kind of movement like climbing and then i write all that climbing logic in the script i stick that script on here and then i just say turn it on and off when we start and start climbing as for the input if i go to my inputs controls we see here i've got this action map and i've actually set it up for keyboard mouse ps4 and xbox so if i was to plug in a controller well it works with keyboard was it also works with the left stick on a on a xbox and a ps4 and jumping or sorry looking is either with my mouse or with the right stick on the xbox in the ps4 and then jumping is either the space key the a on an xbox or the cross on a ps4 and it all just works based on which controller you're using and that feeds into an input script so if i look over here i've got this input reader script and this one is purely just just for you know dealing with input so it just says here jump start jump cancelled so that lets me have longer jumps and when i look and when i try and move so this is purely just for input and i'll handle like rebinding keys in here and these just raise these various events for things happening and then i've got the input binder which actually binds you know the different inputs to do different actions so it says over here you know based on the camera position every frame update the the movement vector because i move relative to the camera and i store the movement input whenever i try and move whenever i jump i pass that on to the jumping script and whenever i cancel jump i pass it on to the jumping script so that the jumping script itself doesn't have a clue about my input which means i can reuse it for enemies it's not bound to certain keys on my keyboard or on the controller so theoretically all of this sliding walking and jumping is reusable for enemies and hopefully the same when i add climbing so i'll probably do an update video at some point when i make enemies and hopefully it all works they should hopefully be able to slide down slopes and walk and jump and if that all works then it just shows you the huge benefits to making or writing reusable code just saves you time saves you time fixing bugs as well because if you have a bug for your player and you fix it then it'll fix it for everything else that uses the same script there's also this other script over here which just sits on the player and every at the end of every frame it just gets the velocity of the character controller and rotates to face it on the x and the z well it rotates on the y but it uses the x and the z so the player doesn't rotate to like slide down the slope on their back they stay up on their feet and obviously it will look better with an animation or maybe using inverse kinematics to place their feet on the slope you know there's a lot of cool stuff i could do with this project and it's just something i'm working on in my own time and don't get me wrong there's many ways to make character controllers and i'm not trying to say this is the best that exists but out of all the ways i've tried in the past i think it's the best out of those ways because i've always ever written one script that does all the movement but then as soon as you want some other thing to do movement differently it's just a pain because you have to rewrite an entire script or use inheritance and you know it just gets messy and i feel like using a state machine makes it so much better and bolt state machine is much better than the built-in unity state machine for doing this kind of stuff the built-in unity one is designed for animation primarily so you still have to use it to do animation so that's why i would have these running side by side so i'd have an animation one set up and this state machine would just call that so if you imagine that i had an animator all i'd want to do is go over here and i'd add in an animator so animator add of type animator and then in let's say the falling state when i leave it down here i could just add in some stuff here and say well for the animator which because i haven't saved it to the prefab let's just head back i need it on here animator and then now i don't have one but it's fine it should still allow me to do all this so i can drag in the animator and then i can say over here like animator dot play and then all i need to do is pass in the name of the state or you can even you know set ball you can still use all the other ways of transitioning so i could say okay just before i trigger the transition play this animation and then trigger the transition so you can do all that and then over here i can just say play the state landing or whatever landing is now it's gone orange because i haven't got the animator yet it's still null but it's just a proof of concept it's really easy for me now to say you know oh yeah when this happens also add this in without having to go in and start writing stuff in these different scripts so yeah i think that's it for this video it was just really a way to show you what i've been working on and to hopefully maybe give you an idea of how to do your own character controller if you're stuck on that kind of thing and i know a lot of you voted for this on the poll so i'm hoping quite a few of you were interested on how to do this with the animator and with the vault state machine i know a lot of you also wanted a video on ui builder and i'm still going to do the ui builder video coming this week instead so the poll was just to see what people wanted first and it also does give me time to plan that one out because it does need a bit more planning and i'll be showing you how to like take a blank project and actually make some ui using the new system from that so we can build a health bar or a menu screen or just something simple to start off with then i can do more complex tutorials after that so i think that's it for now thanks as always for watching let me know down below what you want to see next if you like the video then please leave a like and subscribe i'll see you in the next one thanks for watching and goodbye but of course before we go i've got to thank my patreon special thanks to kate income on francisco lira liz kimber louis ramos jake nixon benjamin hilde san marcos matt fryer malvin john selig david mcdermott exit verdai dustin miller wreck and juris letter if anyone else is able to help support the channel monetarily link to my patreon is down below as well as links to other social media such as twitch twitter and discord if you guys help us out by following on any of those or checking any of those out that'd be greatly appreciated and i'll see you in the next one goodbye
Info
Channel: Dapper Dino
Views: 18,985
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, Shader, AI, ML, Scripting, Mirror, Server, Client, Unity multiplayer, Unity 2020.2, UI, Open projects, Unity, State machine, Character, Controller
Id: SvNCTdTRr1c
Channel Id: undefined
Length: 15min 10sec (910 seconds)
Published: Sun Oct 11 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.