Modular Character System in Unity (Move, Attack, AI, Pathfinding)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this video is sponsored by ad mix in this video we're going to make a modular character controller we're going to set up our character in such a way that it's composed of various scripts this way we can easily swap out a different movement system different animation system swap layer control for enemy AI and many many more this is also an excellent learning exercise in writing clean code let's begin [Music] hello and welcome I'm your code monkey and this channel is all about helping you learn how to make your own games with nf2 torrents made by a professional indie game developer so if you find the video helpful consider subscribing ok so here's what we're trying to make over here is my player character and it's working perfectly normal so I can move around with the keys and I can do some basic sword attacks now for the really interesting part I can pause the game and over here we can inspect the player game object and you can see that it's composed of a whole bunch of different scripts each script is essentially a module executing a specific type of behavior so for example I can move the player movement keys which handles movement by key inputs so I can remove that one and now replace it with the player movement Mouse so now here if I use the keys and nope I cannot kill anywhere but if I right click yep there you go now the player is using a movement system based on the mouse position by clicking so I have swapped out a completely different movement system by just simply swapping a script now for the attacks I currently have some very simple sword attacks and again here the player has a sword attack script so I can get rid of this one and let's say I put the punch attack and said and I'll back in here and I click any up now I'm attacking using punches and kicks and now again swap this one for the projectile attack and yep there you go now I'm shooting projectiles with my player and with the right click I get some move and I could swap it out for the keys if I want it then for the movement I also have different types of handling movement so right now the input is with the right mouse button ok and right now I'm using a movement script which moves directly towards a position so for example if I go through this wall and there you go he just bounces towards the wall and he stops there but again I can pause and use a different module so we're here instead of using the move position direct which goes directly towards the position I can swap it out and it said use the move position pathfinding module so here I am now if I right-click on the other side wall yep there you go now he's correctly completing the pathfinding so instead of using a module that goes directly I use the module that completes it path and now in here I'm using pretty much the exact same basis for the module system in order to create an NPC that works by itself so that one does not have any player input scripts instead over here on the NPC you can see that it has the move Waypoint script so he's moving two waypoints and again I can remove this and instead I have the Rome module and now from look and now he roams around randomly around a certain position and again he is using pretty much the same type of scripts so he's also using the move position that the player can use move transform velocity as well and a character based for the animations everything in order to continue building your games in the long run you need to think about monetization admix is a great tool to help you solve that with admix you can place unintrusive integrated ads inside of your game so instead of an ugly banner ad you have a billboard right inside your game this is especially useful for monetizing AR and VR content without breaking player immersion and since the ads are less intrusive that leads to a better player experience which leads to higher retention which allows you to make more money so you can keep building your awesome game the tool is extremely easy to use just download the unity plugin and import it into your scene you'll see this window where you can select from a variety of sizes and drag and drop the ad mix object into your scene the object is very adaptable so it can fit any surface like something flat or put it around the pillar as you can see it's extremely simple to use and having in-game ads is a lot better than annoying banner ads once you've set up all your ads click this button to go to the website to validate your app once you do you don't start generating revenue in under 24 hours so ad mix is an extremely easy to use tool that works better than standard ads unless you start earning revenue immediately go to ad mix dot in and download the end mix unity plugin to start earning revenue today thank you to add mix and also thank you to these awesome supporters for making this video possible go to patreon.com/scishow to code monkey to get some perks and help keep the videos free for everyone so here is a very nice modular character system now how much only ego depends on what type of game you're trying to make so you can make this extremely modular or just a necessary amount so for example you can also make the input modular or have it baked into the movement handler so it really depends on what you're trying to accomplish here the character is built in a very modular way you can easily make your own custom modules with whatever behavior you want this is also an excellent system for learning about cling code by separating logic into modules it forces you to think about how to best decouple all of your systems so even a you're not interested in using this specific type of system in your game this is still an excellent learning tool all right so this circle let's get to it okay so here we are in our stirring scene all I have is my basic character sitting around doing nothing essentially the only module I have pre-built is the animation system which is my custom animation system that I always used in the videos is based on dynamically modifying meshes in runtime so if you want to learn more about how it works check the video linked in the description so over here is the game object and the connection with the animations is done so only through this script so again I could replace this script with another one that works with a completely different animation system and everything would work perfectly alright so let's begin by handling some basic movement first let's make a script to handle setting a velocity vector so here let's make a new c-sharp script let's call it the move velocity now this script is going to be solely responsible for setting the velocity vector on this particular game object so here pretty much all we're going to have is a function in order to set the velocity all right centered is we just have this function and we set our vector now in this case we're going to move our game object using a rigidbody in saying the velocity so let's use this vector on our private roid fixedupdate we simply go into the rigid body so let's grab it alright so just like that we were at the rigidbody and we set the rigidbody velocity to this velocity vector so this way we're going to move the rigidbody now let's expose the field for the speed and let's also play the animation and yet there it is just like this so here is our entire move on a three script so as you can see it's extremely unlimited it has one purpose and one purpose only it sets the velocity in order to move the object now let's make a player input script so let's make a new c-sharp script call it let's say player movement keys we're going to move the player using the key input so here let's do some very music input code all right here this all we're doing is checking for the keys on the various directions and constructing our move vector another we have the move vector and let's pass it on to the movie script so we just do get our component type move velocity and we set the velocity to this move vector okay so that's it essentially here the move on a three does not care who is calling this function it just takes the velocity vector and uses it in order to move the game object okay so our simple script should be working now let's go into the editor and here let's just set up our game object so first we had the movie script then we had the player movement keys here and let's set a certain movement speed and now we also need the rigidbody 2d and we should probably also add a simple toy alright alright so here we have our game object set up with all the required components let's dust okay so here we are let's press the keys and yep there you go he's walking correctly alright so the player is moving exactly as intended awesome however our code isn't really very modular just yet we have one player input and one movement script so right now this movement script is working with the rigid body physics but let's say we want to work directly with the transform dot position instead so here let's make another type of movement script well let's create a new c-sharp script call this the Move transform velocity and now here let's pretty much copy the move Austrey script so just copy all this except we are not going to work with the rigidbody instead we are going to move the transform directly so instead of our fixed update let's make a normal update and just move the transform that position using our velocity vector by our move speed and time dumptown alright so that's pretty much it so we have this script working with physics and this one just moving the transform directly so here in our player script let's remove the move velocity component and replace it with our move transform velocity module and now if we test there your an error instantly essentially we have a known reference exception and if we look into this line over here is our error essentially the player expects that this object has a move on a three-component and not a move transform velocity component so our code isn't really modular just yet now the way that we solve this is by making a nice interface so here on let's make a new you see sharp script and we're going to call this the eye move velocity and now in here this will not inherit the mana behavior and it will not be a class but instead it's going to be an interface now if you're not familiar with interfaces they are actually very simple instead of defining methods and implementation we just defined a method signature then all the objects that implement this interface must implement those methods and the name usually by convention starts with an I so I move on interfaces are really interesting so let me know if you'd like to see a dedicated video on them now here what we're going to do is implement this same interface on both the move elasticity and the move transform velocity scripts and 40 functions let's make sure they both implement a simple void set velocity so every single script that implements this interface must also implement this method so let's go into the move on a city script and here we're going to extend monobehaviour yes and also implement I move a city and here we already have this method so we already implemented our interface and the same thing for the Move transfer velocity so in here we also implement this alright so now we have both scripts implementing this same interface another way that we solve the error that we have is over here on the player movement Keys instead of getting the component of type move on a city we're going to get the component of type time move velocity so our interface rather than a specific script so this group doesn't really care about which script he's going to use the set velocity function so it can be either this one or this one it doesn't matter as long as they both implement this same interface okay so let's test so here we are and let's move and if there yo we are indeed moving and if we inspect we can see that we are moving using the move transform velocity component so now we can swap this out so get rid of this one and send let's add the move velocity and let's see and if there you go we are still moving except now we are moving through the rigid body and now to see the difference between physics or no physics let's have a simple one so here is a basic one it is a sprite with a box Collider now let's see so here we are currently using the movement system that works by modifying the transform directly so if I go through the wall yep there you go I can go straight through the wall because again we are moving by modifying the transform position directly however if we pause and now swap out the move transfer velocity by our move velocity and now we test and now we move and there you go now we can no longer go through the wall because we are now moving using our velocity in the physics so here we have an example of modular code in action we have two scripts that have different behavior but they share the same interface and then we have another script that interacts with whatever script is present through the interface all right so now that we've seen the basics let's expand upon this concept now let's make a script that moves to a position so let's make a nice sharp script call this smooth position direct so this grip won't take a position and move the unit towards that position directly all right so here we have our very simple script it just exposes a function in order to set the move position and then on update we simply use the movements in the interface in order to set the velocity to move towards that move position now let's make a player mouth script so a new script call this the player movement Mouse and now in here let's listen to our part void update input get mouse button down on the right mouse button so when we press the right mouse button let's down that script in order to move to the mouse position all right so there it is very simple here I'm using the mouse run position from the code monkey Tony's which is almost he can grab for free from in Tacoma comm and if you want to implement yourself here it is just doing a worm camera screen to run point so just like this everything should work we have this script listening to a right mouse button down and when it happens it passes in the mouse wrong position onto this script and then this script receives that position and uses the movie mastery interface in order to set the velocity to reach that position so back in the editor let's use these modules so let's get rid of the player movement keys and instead we're going to add the player movement Mouse and we're also going to add the move position direct so we have the point movement mouse move position direct and also knee move transform velocity let's test ok so here we are and if I move using the keys if there you go no movement so we are not listening to key input but if I go in here and I right click yep there you go the character moves towards that position so now we have movement being handled through the mouse so here it is just like that all right great however we have one issue which is let's go up here onto our wall so let's go in here and I click on the other side and there you go it's moving directly through the wall and if we add the physics movement module so remove the move transform instead use the move velocity and I click and there you go I'm going straight through the wall so this rift is trying to move straight to the target once ignoring any objects so once again let's make a different movement script and on this one we're going to use path finding so let's create a new script let's call it move position cat finding and now here we're going to have the exact same function so a public void set little position and now in here instead of going directly like we did on the other script we're going to use path finding all right so here it is so we have the same set move position function except instead of moving directly we're completing the path finding I covered a star path finding on a previous video so check the link in the description then we simply have a simple path finding movement so we get the next path position we can only move velocity we set our velocity in order to move our transform and then we check if we are near that next path position if so we increment the path and if we reach the end we set it to minus one which makes them go idle so there it is very simple and now let's see the same thing that we did with the move on a three so let's make a common interface between this one and the move position direct so let's make a new script call this the I move position and now this one is not a class-four rather an interface and all we're going to have is a void set move position and yep that's it that's our interface and now on the move position path planning we implement it which already has this one and only move the right we do the same thing all right so now we have this interface being implemented in two separate ways so one of them goes straight direct towards the target position and one of them uses path finding in order to calculate the path so now all we need is to go into our player movement Mouse instead of using move position direct let's used the interface alright and that's it and everything should be working now here in the editor and let's swap out the move position direct for the move position pathfinding so now let's see if we right click and it goes around our walk let's see okay here we are and if I click yep there you go I am still moving towards the mouse position and if I click on the other side the wall yep there we go he's correctly going around the wall so here with a bunch more walls just to see let's right-click on the other side any of there you go he come likes a path and he goes around it and once again if we swap it out so here the player using the move position path finding so let's get rid of that and go back into the move position indirect and now here and right click directly there any of there you go now with this script with this much he's trying to go direct instead of complaining the path so you can see how we have completely different movement logic by just swapping out one module for another one now with these examples you can already see how we can build a modular character so essentially we make several modules each of them only with a specific single purpose and we make all the interactions going through interfaces so we can easily swap out components for other components with different behavior all right now let's expand upon this concept even further okay so now here I found the same logic to build a bunch more modules so for example I have a sword attack punch attack projectile attack and those implement the eye attack interface and here it is pretty much the same pattern so we just have our interface and our attack method so here the player has the movement key script as well as the player attack which is just listening to mouse input and right now we have the projectile attack module so let's see this okay so here I am and I can move around with the keys yep perfectly fine and I'm using the physics module so I cannot go through all this okay great any file left-click and there you go I can now shoot some projectiles so there you go just like that and now if we pause and now let's swap out the projectile attack for the punch attack and here we are now if I click if they're going on with tagging with punches and kicks so again following the same thing now we have two different attack behaviors and over here I have an NP scene so it essentially has no player input and it has some different logic scripts so if you pause and look at him there you go he has the Move olicity and he moved position direct and then he has this move roam component so here what we're doing is just calculating a random position and we're using the I move position in order to set that move position so he goes towards it once he gets there then he gets a new random position so you can see that he has no input components at all so they're both using the same marginal things so they both use the move velocity they both can use the move position direct and they both have the same animation system but over here I have the player input scripts and that one has an automatic similar logic AI and again I can swap out the modules for different ones so instead of the Move Rome let's add the move Waypoint script and here it takes a list of waypoints so this script has logic going from one main point to the next and then let's also replace the move position direct for the move position and pathfinding now let's see and there you go now the NPC is working with a different logic just roaming from one position to the next and once again I'm back to shooting some projectiles which I can easily swap for different behavior so here you can see just how crazy modular our system is already and by building this system we learn some more about the importance of writing clean code with as few dependencies as possible most of the components that we built are completely decoupled from any other system so using this we can easily make just about any type of behavior we want we can make a player character that attacks using a sword and moving with keys or we can make a ghost enemy that goes through walls without using physics and roams around an area looking for the player while moving using path line so here you see a very versatile and very modular way of making a nice character controller with as many different modules as you want thank you to add mix and also thank you to these awesome supporters for making this video possible go to patreon.com/scishow new code monkey to get some perks and help keep the videos free for everyone as always you can download the project housing utilities from unity karmic calm subscribe the channel for more ent tutorials post any questions have in the comments and I'll see you next time [Music]
Info
Channel: Code Monkey
Views: 48,850
Rating: undefined out of 5
Keywords: unity modular character, unity modular, unity character controller, unity character, unity interface, unity pathfinding, unity movement, unity ai, code monkey, brackeys, unity tutorial, unity game tutorial, unity tutorial for beginners, unity 2d tutorial, unity, game design, game development, game dev, game development unity, unity 2d, unity 3d tutorial, programming, coding, c#, code, software development, learn to code, learn programming, unity sword, c# interface, unity3d
Id: mJRc9kLxFSk
Channel Id: undefined
Length: 21min 15sec (1275 seconds)
Published: Fri Mar 06 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.