2D Top Down Movement UNITY Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
to DTaP town movement we're not talking about this game we're talking about this game and here's how you can set it up pretty quickly so we have our empty 2d unity project as usual and the first thing I'm going to do is select on our assets folder head right click and go to import new asset and import what I'm gonna use as a player sprite I'm just gonna use the 64 by 64 pixels square feel free to use anything you want and we'll just drag the square into the scene so we'll click on our square and in the inspector we'll rename this game object from square to player there's a lot of ways of handling movement from a 2d perspective you can directly translate things across the screen you can use unities built-in physics engine you could use an external physics engine some third-party library or something like that we're gonna be using unities built-in physics engine and in order to do that you need to add a rigidbody to a game object so we can come down here to this button add component and select rigidbody 2d because we're doing a 2-d game when you play the game by default you'll notice the square falls off the screen this is because our gravity scale is set to 1 first thing we wanna do is disable the gravity by setting this from 1 to zero we also want to expand this constraints tab and make sure freeze rotation in the z axis is checked this helps later on so when you bump into objects or you get shot by projectile something like that your character won't just start spinning we can then right click on our assets folder again and right click and create c-sharp script and I'll call this player movement and we could just drag this player movement script onto our player and you'll see it appear before we dive into the script though let's go into our edit project settings and then our input manager tab we can expand axis and you'll see horizontal and vertical if you expand these you can see a whole bunch of different inputs like the left arrow key the right arrow key the ad it's worth noting that unity at this point is redesigning their input system by the time you're watching this video they might already have a better solution but this is how it's been for many years so you can configure these different inputs with whatever buttons you want but in order to make our player move will be utilizing these in our script so speaking of we can go down and open our player movement script so we need a few variables to control how our player is going to move the first thing we want to be able to tweak is the move speed and for this a float is much better than an integer because we can fine-tune the value with places so let's make a public float movespeed and then we also need a reference to the rigidbody that we just attach to our character but remember that it's a rigidbody 2d so we can say public rigidbody 2d RB so when we save and go back to the editor after it compiles will now see a movement speed and a rigidbody section open up so we can take this rigidbody 2d and drag it into the variable and let's set this movie to something like 5 just so it's not 0 for now we'll get rid of our start function but we'll leave update so in unity there's two types of update functions there's your regular update but there's also another one so if you could type in void fixedupdate these work differently update is driven by the framerate of the game as you are playing so this update loop actually gets called differently depending on what your framerate is at the time where as fixed update gets called a set amount of times per update loop which makes it very consistent what this means is that updates good for processing inputs but fix update is where you want to do all of your physics calculations and if you think about it that should make sense if you're trying to calculate velocity but your values are inconsistently changing you're gonna have like very erratic gameplay whereas if your calls are always going to be consistent then you never really have to worry about variation in that sense so to emphasize this point I'm gonna make two new methods I'm gonna make void process inputs and another method called void move okay so in our process inputs method we basically want to check the inputs that we looked at before in the editor so we'll make a float move X and we'll set this equal to the input dot get access raw get access gives you a value between 0 and 1 whereas get access raw only gives you 0 if you're using like an Xbox controller and you were tilting a joystick you might get somewhere in the middle between 0 & 1 like 0.5 or something get X's Frye is just strictly 0 or 1 so we'll leave this as get access raw and we'll actually type in horizontal which was the input before that had a and D and the left and right arrow keys so we'll do the same for vertical we'll say float moved y equals input get access raw vertical oops also I notice I just typed this wrong my bad yeah it's supposed to move okay so we want to actually persist these float values into a vector and be able to call it in our move function so we need to make another variable up top so make a private vector two and we'll call this move direction and so back in process inputs we can now say move Direction equals new vector to of move X and move Y for those of you in the know we're not done with this yet so we'll expand on this in a little bit but for now this is all we need and in our move function we can say our B which is our rigidbody dot velocity is equal to new vector to move Direction X multiplied by our move feet and then in our Y value move Direction Y also multiplied by our move speed in our update function we can now call process inputs and in our fix update you guessed it we can call move using my square spread I just made some walls around our player and now we can test and so now you can actually move around the scene using WASD or the arrow keys this is great and as I alluded to just before there is one more thing we still need to take account of if you move in any of the cardinal directions of north east south and west the movement speed is pretty consistent but once you start moving in a diagonal you'll notice it moves significantly faster this is because of how vectors work in general when you're moving in a diagonal you're actually adding two vectors at the same time and so when you actually calculate the math I think it ends up being like 1.4 times faster than just going in a single direction alone and this is the last thing we really need to take care of so back in our script in our process inputs method it's very simple and what we need to do here and unity actually gives us a nice way of handling this after we create our new vector2 we can simply say dot normalized and if you look at what normal eyes does it says it returns the vector with the magnitude of 1 our move Direction would actually generate a vector with a magnitude greater than 1 when you were moving in two directions but now it caps that to one meaning that every direction you move in including the diagonals will now always be 1 times whatever your move of speed or negative 1 times remove it speed or 0 if you're not moving but this is what makes the movement assistant and it makes a big deal in a top-down game so make sure you're doing this unless you purposely watch try moving to be faster which could be fun if you design the right game around it is it a bug or a feature you make the call so with that said this wraps up our movement tutorial I know we didn't cover your other things like collision or animation but maybe I can follow animation up in another video if this helped you out like the video and leave a comment if you have any questions and if you want to see the follow-up videos or see any other game development content make sure you subscribe so [Music] thanks for watching [Music]
Info
Channel: BMo
Views: 76,490
Rating: undefined out of 5
Keywords: Unity, Unity2D, Unity3D, Tutorial, Unity Tutorial, Beginner, Top Down Movement, TopDown Movement, Top Down, Movement, Player Controller, Character Controller, 8 direction, bmo, learn, how to, Move in unity, 2D Top Down Movement, tips, development, game development, game, video game, basic, basics, c#, roguelike, rpg, input, system, rigidbody, rigidbody2d, controls
Id: u8tot-X_RBI
Channel Id: undefined
Length: 7min 21sec (441 seconds)
Published: Sun May 31 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.