Master Grid Placement in Unity 2022 P1 - Calculating Cell Position

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this tutorial series we are going to create a grid placement system in unity where we can place objects on the grid as well as remove them if we need to hi I'm Peter and welcome to Sunny Valley Studio tutorials in this first video we will learn how we can convert Mouse position to a grid coordinate system so that we can select a specific cell if you want to follow along I have prepared a starter project that you can grab using the link in the description just unpack the zip file into a separate folder and use the open functionality of unity Hub otherwise create a new project and be sure to select the urp 3D project as a base for the unity package that you will import there when you have the project downloaded be sure to go to the projects tab asset scenes and open the grid placement scene and here we are going to be creating our grid placement logic in our project we have this grass plane that we are going to be using for our placements them so the first thing that we need is to detect where are we pointing using our Mouse at this plane what is the position so let's go to our asset scripts folder and I will create a new c-sharp script called input manager and let's open it up okay we're going to have three Fields here a serialized field private camera scene camera so that we can rake us and detect our Mouse position private Vector 3 last position so that we can catch the last position detected and we are going to have a serious filled private layer mask placement layer mask so that we can decide which plane takes part in our input detection of the mouse position on our plane we are going to create one method called public Vector 3 get selected map position so here we are going to get a vector 3 Mouse pause equals input.mouse position we are using the old input system and we are going to set Mouse position dot Z equals scene camera dot near clip playing so that we cannot select objects that are not rendered by the camera next we are going to create array array equals scene camera that's why we need the reference screen point to Ray and we are going to create a screen Point array from the mouse or position when we have it we are going to create a raycast hit hit this is the result and we are going to use physics raycast to detect the selected position on our plane so we're going to check if physics Dot raycast and we are going to pass the array out hit so the result we are going to pass 100 as the length of our raycast and we are going to use the placement layer mask to detect only the plane that we want to use as a grid we are going to cast this as a last position equals hit dot point if this is correct if not we are going to Simply return the default values of vector 3.0 this will help us to return some value even though we are pointing our mouse cursor over something else that is not on our layer mask okay let's save the script let's go back to Unity now there will be a GitHub link to the scripts if you need some help with those so to use this we are going to need to have another script called placement system so let's right click create a new shop script we are going to call this placement system and let's open this script app we are creating a new script to keep our code nice and tidy we are going to create a serialized Fields attribute and we are going to create a game object but it will be called Mouse indicator we are going to use it to visualize which position on the plane we are selecting next we are going to have a serialized field and we are going to create private input manager we are going to need it so input manager and we can add private field in front of the game object just for the sake of consistency next we need to have update method and we are going to call here Vector 3 Mouse position equals and we are going to call input manager dot get selected position and next we are going to call Mouse indicator transfer position equals Mouse position okay let's save the script let's go back to Unity great in our hierarchy we have this building system apparent game object let's right click on it and create an empty object and let's Ctrl D to duplicate it first one we are going to rename the input manager and the second one will be placement system okay we are also going to add here right click and add a 3D object sphere this will be our temporary Mouse pointer let's select in the inspectorate scale let's toggle uniform scaling and let's scale it by 0.2 so this will be the scale so we can see this object but it doesn't take the whole cell space on to the input manager we are going to drag the input manager script and we are going to assign to this screen camera so in general objects we have this main camera object let's assign it for the placement layer we can for now select the default layer since this is the layer on which the grass plane is right now we are going to modify it later okay we have everything for the input manager our replacement system let's drag the placement system script and we need to assign the mouse indicator so this is our sphere and the input manager this is our input manager I'll select our sphere and let's set it a layer to be for example water so that our detection system does not detect the sphere itself okay I think we are good so let's press play okay great and now we should see that our sphere is following our Mouse pointer so that's great we have the input detection on the plane now we need to find the cells that are assigned to each piece of land on our plane and we can create this grid placement system to easily convert our world position to grid space we are going to use the grid component it is a guide that helps us align game objects such as style based on a selected layout and if we go to the switch to scripts we're going to find that it has two important methods cell to World which converts a cell polish into World position and the world to sell which converts the world position to cell position so we can easily convert our position using this component if you are enjoying this video so far it would really help me if you would click the like button or post a simple comment like thanks right now under the video it really helps with the YouTube algorithm in our project let's go to the hierarchy building system let's right click and create a new empty game object we are going to call it green aren't I'm going to explain why in a bit but the important part is that we place our grid inside this so we are going to right click create an empty object we are going to call it grid and we are going to add a component and we are going to search for the grid and here it is here we can set the cell size on the X Y and Z one is optimal since it represents one unit so if we add a 3D cube this is one unit the size of the cube is one in the unit okay and we can modify the cell layout to be hexagonal if we want to we are going to leave it as rectangular with this done we are going to want to modify our replacement system script before we go and do that we need to find in the prefels folder inside our projects tab we have those different refurbs here we have the cursor indicator parent we are going to use it to indicate where is our grid cell because right now we can't really display our grid on top of our plane so we are going to use this so let's drag it into our building system for now and this will be representing our object on the grid right now this is Z fighting with the grass plane so let's select the child object of this and we are going to set the Y position of this to be 0.05 for example so now we can display it on top of our grass plate great so now everything is prepared let's go to our scripts and let's open our placement system great here we are going to add another game object reference this will be our cell indicator and obviously we need to add a serialized field this will be referenced our grid so private we read inside our update function we are going to now convert the mouse position to the grid space so we are going to create Vector 3 int grid position and we are going to set it to be equal to our grid and we are going to convert world to sell this converts a world position to sell position we are going to pass to it a mouse position and we would use it to save data about our grid but for now we are going to create another line cell indicator transform position we're going to move it to our grid cell to World we're going to select this method converting the grid position back to World position but now it is the cell position and not the mouse position of our cursor so let's save this and now let's go back to Unity great let's select our replacement system in the hierarchy and let's assign our grid to where the grid reference is and our cursor indicator parent where is the cell indicator now we should be good to go so let's press play okay and now we should see that the cell cars are or cell indicator is not moving with our Mouse position only when we move to the next cell it is being moved and now we can see where are the cells and where is our Mouse pointer so if we are selecting position within this cell we're going to select only this cell if we go to the right or to the left or whatever we are going to select the next cell and this is how we can create a simple grid placement selection one thing that I want to point out is that if we select our cursor indicator parent we're going to see that it has its pivot point in the bottom left corner so this is how our excels are represented inside our grid so they are represented as the bottom left corner so if you have an object that has a pivot point in the center you need to calculate this position based on the grid cell size just to explain why we need this grid inside the grid pattern object if I leave the indicator somewhere on our in our game and I will select the grid pattern in our hierarchy and start moving it you will see that now the cell position is being updated for our grid indicator so basically we can adjust our offset of our grid by moving the parent of our grid which can be useful if you want to move our grid if I set the position to be 0 and 0 and if my grass plane is a 0 0 as you can see the position that we have selected adjusted to the default grid that UNT presents Us in every project this is because the center of the plane is at zero zero zero and the grid is placed at zero zero zero so it starts selecting the positions with the layout over the default grid inside Unity it is pretty important when we want to move our grid to some other position on our map in the next video I want to create this grid overlay using a custom Shader so that we can see where are the grid cells on our grass plane if you want to learn how to use this grid placement system in a more advanced house building project I have this house building system unit in projects template the link will be in the description if you want to check that out okay see you in part 2 of this tutorial
Info
Channel: Sunny Valley Studio
Views: 85,280
Rating: undefined out of 5
Keywords: grid placement system in unity, unity grid cell position, grid component unity, mouse position on plane unity, unity grids, unity grid placement, Don't make grids without knowing THIS in Unity
Id: l0emsAHIBjU
Channel Id: undefined
Length: 12min 5sec (725 seconds)
Published: Thu Mar 23 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.