Mouse Click Movement in Isometric Tilemap - Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'll be showing you how to do an isometric 2d movement controller script so that you click and then the player will go to that clicked position in your isometric map and this will be using the new input system alright so the first thing that we're gonna want to do is download the new input system so go to window package manager and then we're gonna search for input at the top and here we are the input system then we're going to download it down here it's gonna say install and you just click the install button and then it will ask you to restart the editor and you will click yes so that it can apply the new input system so the first thing that we're gonna want to do is make a character for our game so you can have your own imported sprite character but in my case I'm just gonna right click and create a 3d object and I'm going to create a capsule and then in this capsule I'll decrease the scale to about 0.25 on both sides so we can make it small and then here we're gonna want to delete remove this capsule Collider from its inspector and we're gonna want to add a 2d capsule Collider because we are in a 2d space and we want to make sure the colliders are working properly and now we can actually write a script for our object so we can right click on the assets create and we can create a new folder we're gonna call it scripts and in there we're gonna right-click and we're gonna want to create a new input action so this is where we'll see our mouse movements so I'm just gonna call this Mouse and double click that so we want to be listening to two things we want to be listening for a mouse click so we can add in a action map and the action map we'll just call it Mouse so action maps you can actually have different action maps and you can switch between controls depending on your action map selected so it's might be useful if you want a different control if you're on the menu versus on the map itself and then we have our actions we can we already have a new action here we can press f2 to edit the name and we're gonna call it mouse click so it's gonna be listening for a mouse click and then under the mouse click we can click this button here and we have a binding here this is where we can actually add the control to listen to so under the path we can goto mouse and then we can do left button which is our clicking button you can also do right button if you want or any other button when we click it we're gonna get an event that we're gonna subscribe to and then in that event we also want to take into account the mouse position so we're gonna make another action called mouse position and then for the mouse position we actually want to return a vector2 because the mouse is in an X and y position so it's two values so vector two so we're gonna switch to value and then in the control type we're gonna press vector two and then in the binding itself below it we can go to our path we can go to Mouse and then we can select position so that will return the position of the mouse at the time we call it so then we just click Save asset and that'll save it for us you can also click Auto Save up here if you don't want to keep saving it every time you make it change and then we can click the input action itself in the scripts folder and then we can generate a new c-sharp class we can call the class named mouse input so we can differentiate it from the other one and then we have to make sure the class name equals the file name so we can press these three dots here next to the class file and we can just put in mouse input and click Save and that'll just save the file as mouse input cs and click apply all right so now we have a our script for our mouse input we can right click and create a new C sharp script and call it our character movement and we can double click that all right so here's our script the first thing we want to do is get a reference to our mouse input so let's say mouse input mouse input and now we have to have an awake function and in our awake function we will initialize this mouse input cell mouse input equals new mouse input and then we have to have an on in able function so this is called when the script is enabled and we have to say mouse input enable and then we also have to have a on disable function and there we will say mouse input disabled so when the script is disabled we also disabled the input action and then in our start script we have to subscribe to the event where we click and we can do by saying mouse input dot our action map which in this case is mouse dot mouse click dot perform so this will be called whenever the mouse click is performed or finished plus equals underscore greater than equal sign mouse click which is a function that we will create right now so this is just saying once it's been performed then we're gonna be calling this function and here you can actually put in a variable we can just call it context and then you would actually be able to pass in the context by saying context that read value and you can pass in the value itself that's right from the mouse click but in this case we don't need that since our mouse click is just calling the function when it's performed we're not having to pass in anything like a vector2 and then let's make a private void mouse click and this is the function that will be called and then in here we can get the position of our mouse so that's a vector too and we're going to call it mouse position equals and then we can say mouse input Mouse da Mouse position so this is the action that we created and then we can say dot read value and then we can pass in our vector two so that's the value that we want to be reading and to get the values from an input asset this is the syntax that you need to follow so currently we're getting the mouse position and this returns the mouse position in pixel coordinates so unity has a type of coordinate system called pixel coordinates and the bottom left of the screen is at 0 0 and the top right is at the screen width and the screen height so this just returns a value between 0 and the screen width or height depending on the X or Y but we don't want that we want the actual point in our 3d space currently it looks this looks 2d right but this is still a 3d representation so if we unclick 2d we can still see that we are still in a 3d space and we have to accurately convert those pixel coordinates into this world space which is where our map and character live on and we can do that via a simple function that unity provides to us so that we can replace the mouse position equals and we can call camera main screen to world point so that converts the pixel coordinates to a world point and we can pass in our mouse position this takes in a vector three so this casts the vector two into a vector three and it will just put be easy access to zero which is fine since it's a 2-d game we don't really need to worry about the z axis a little optimization that you can do is that every time you call camera main unity has to go and find the camera so you can actually keep a reference to the camera in your scripts so if you call it a lot you don't have to keep saying camera domain all right so now that we have the world position we actually have to get the cell that it's that we're clicking so uni has a function for that that they include in their tile map class so we can say at the top we can say using unity engine tile maps so we can get access to this and then we also need a reference to the map itself so at the top here we can say public tile map map or you can just make it private and make it a serialized field so and the editor will put our reference to our map and you have to make sure the map has a Collider on it or else unity won't be able to tell if that point is on the map so now that we have a reference to our map we can say vector 3 int and that's just a vector 3 made up of ins grid positions so this is the position in the grid and we can say map dot world to cell and we can pass in the mouse position which is the world position so we convert the world coordinates into the tile map coordinates and the reason I'm doing this so you can actually just move the character to this Mouse position and it will go to the position of your mouse right but then you can be able to click outside of the grid and the character will still be moving outside of the grid so if you actually want to keep the movement inside of the grid so you want to make sure that they're clicking a tile then you have to do this you have to get the grid position and then we can check if the map a tile at that position so we can say if Matt duck has tile at that grid position then we can move to that position so how are we going to move to that position though so we can actually do that in our update function and we can keep a reference to the position that we want to move to up here so we can say private vector3 destination and that's the destination that we want to move to and then let's actually initialize that value so in our start we can say destination equals and then we want to set our destination to our current position so that we're not moving at the start of the game so destination equals transform position and then down here in this if statement for the mouse click we can say destination equals Mouse position so this is the world position and this is just making sure we are clicking the self but if you put colliders around your map then you can have this because the player will just like keep walking towards the wall but it looks kind of weird if your character is just walking towards a wall you can also make it so that the character stops once they reach a wall but this just makes sure that we're in the grid itself awesome then in our update function we can actually move the character so unity has a function called move towards and you can put in your current vector which is your current position you can put in your destination and you can put in basically the speed of how fast you want to move to that position so we're gonna be using that to move to our position so we can say vector 3 dot move towards and we can put in our current position which is transformed up position we can put in our destination and then we can put in some kind of movement speed which we will have to specify up here so up here we can say serialize field private float movement speed and then we can set that in the editor and then back down here we have that movement speed but we also have to times it by time.deltatime and we do that because the time.deltatime is the time passed since the last frame since there's so many frames if you don't put that it'll move super fast towards the position almost instantaneously but if you put the time passed since the last frame then it kind of smoothes it out over the time and then here we actually have to equal this to the transform position so it will make the player move awesome but vector moves toward will be called on every frame so we can actually perform a little check before that to make sure we're not at the position so we don't actually have to call that function so we can say if vector 3 dot distance and we can say the distance between the current position and the destination is greater than 0.1 F so I don't want to make it exactly the value because sometimes it never actually reaches the exact position there might be a little offset so we're saying if the distance between these two vectors is greater than 0.1 which you'll barely notice then we move towards that position awesome so now we can minimize this and then our script will compile we go to our capsule we can add in a component and we can add in our character movement script so let's put the movement speed to about two and then we can click this little circle here next to the tile map and then find the tile map that we want to be clicking so we want to be clicking our ground so if you haven't watched my previous video on how to make an isometric map or you don't know how to make an isometric map I cover that in a previous video that I will link below but basically here we have our tile map which is our ground you have to make sure that your tile map they have a tile map Collider 2d and you can also add a composite Collider 2d to make all your colliders go into one to save some performance and you also have to make sure your rigidbody 2d is frozen on the position rotation and your gravity is zero so this is to prevent the tile map from flying around and being affected by physics so then unity is not actually showing my player since it doesn't know in what order to render it on correctly so we can click our capsule and then we can actually change the Z positions so I'm going to change it to negative one so that it's in front of theme app since it's a 3d object if it was a 2d sprite you just changed the sorting layer and now we can press play and then when we click it'll move to our position and if you see we click outside it won't move to that position and then the reason it's not detecting collisions with the tiles themselves like the crates is because we need to add in a rigidbody 2d to our character and let's make sure to freeze the rotation we won't don't want it rotating and let's be sure to set our gravity to zero so now if we click play then it will start to detect collision based on its Collider which is awesome currently you see that we can't get close to the crate because the top of the collider is colliding with it so then in our player we can say we can click it and press edit Collider and then we can just drag the top vertex all the way down so we can make the collider smaller and when we click play it will actually appear to you know be standing in front of it and it won't just stop at the top because that's where it's Collider is and see we won't be able to go on the lake and then you can write a small script if you want to make sure that the player stops whenever it's near the collider so that it just doesn't try to like keep going inside and you can do that by getting the vector between our current position and the destination and shooting array towards that and if there's a Collider in the way then we can just stop so that's just a inclination of how you might want to do that if you'd like so that's the end of this video I hope you enjoyed I want to thank a new patreon supporter on the dedicated tier our money I really appreciate all of the support so far and if you haven't checked it out already I recently made a patreon where I'll be offering source code and also early access to my videos as well as some other benefits so thank you so much if you enjoyed this video please make sure to LIKE and subscribe any of you haven't joined my discord already the link will be in the description Thanks
Info
Channel: samyam
Views: 29,137
Rating: undefined out of 5
Keywords: samyam, unity, game dev, game development, unity beginner tutorials, unity 2020, unity isometric movement, mouse movement, isometric mouse movement, unity iso, unity isometric tilemap, unity isometric tilemap system, unity isometric tilemaps, unity isometric tiles, unity tilemap, isometric, isometric movement, isometric games, isometric tutorial, unity mouse movement, unity mouse movement input, new input system, unity input system, unity 2019, unity new input system tutorial
Id: b0AQg5ZTpac
Channel Id: undefined
Length: 15min 17sec (917 seconds)
Published: Thu Jul 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.