Placement Collision Detection - Grid Placement System in Unity P4

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we are going to implement placement validity check so that we do not Place Furniture objects on top of other objects that are already placed on our map unless we want to like placing furniture on top of the floor house okay let's get going to check for placement Collision we need to store the cell positions that are already occupied by the objects on our map for that we need to create a new script so let's go to the project stop scripts let's right click create a new c-sharp script let's call it grid data and let's open it up great now this will not be a mono Behavior but just C sharp plus you're going to delete the start and update now we need to have a way to store the data about our placed objects so we are going to create a dictionary although it is common to also store this data in an array like structure but dictionary will be better for our purpose the key of this dictionary will be Vector 3 and so that we store all the cell positions as the keys of the dictionary so that we can easily access different cell objects or different cell positions and check the data about them if we have them in our dictionary so the data that we will store so the value will be placement data this is a new type that we are going to Define let's call this placement data or maybe Place placed objects since this will be different from the type set it to be equal to new since Unity 2021.2 opportunity supports C sharp 9 and we can type less when creating those objects by just typing new not repeating the whole definition of a dictionary okay so we need to now Define this type placement data so let's create underneath this definition public class and we are going to type placement data and this will be the data that we store as a value of our dictionary let me paste the fields that we are going to use here here we are going to save the data about the object that we have placed on this position so we need to have a list of vector 3 int occupied positions so those will be the positions occupied by this object now this is a public list not a property because the list is a reference type so we can always modify it and safeguarding it will take a bit more work we are going to also have an ID and a place object index both are int properties with a private Setter so that we can really set those outside of the Constructor of this class the ID is useful when we want to save the data about our map and load it and this will just save the the ID of the data definition from our database now the int Place object index will be useful when we want to create a remove object system now to create a Constructor we can right click on the name of the class quick actions and we have this generate Constructor if you are using visual studio and we can select all those values here and we are going to generate this and we can actually move it below the field definitions so that it is separate okay so this is our Constructor it will assign those values to our Fields here okay so with this done let's go back to our grid data We'll add couple of methods to discrete data we're going to create public void and we are going to call add object and we are going to take here a vector 3 end grid position and we are going to add Vector 2 in and this will be the object size and we of course need to have an INT ID and end object index so this would be actually a placed object index Maybe okay and let's add the braces and we can right click on this quick action and wrap parameters align wrap parameters so that you can see this more clearly on our screen so to add an object we are going to create a list of vector 3 in and we need to calculate what the positions are to occupy so we are going to type positions to occupy we are going to create a new method calculate positions and we are going to pass here the grid position and we are going to pass here object size right let's right click on this method quick actions and generate this method or you can just type it by hand we're going to create our placement data so the definition here is a at the bottom of this data equals new placement data and we are going to pass here our positions to occupy the ID and we are going to pass here the place object index and we are going to look for each we are going to type VAR position in our positions to occupy we probably should check here if our dictionary already does not contain this so we are going to check if our placed objects contains he and we're going to pass the position so pause you're going to throw new exception and we are going to pass dictionary already contains and we are going to add a dollar sign before the the definition of the string this cell position and we are going to pass the position okay if we are safe to add this object to our dictionary you're going to say Place objects and we are going to use square brackets position equals the data so we are assigning to each occupied grid position this data that we have defined here so that we now can access it by clicking on the same cell again okay now the calculated positions method we are going to create a list of vector 3 and return values equals new okay and we are going to create two Loops four I'm going to press Start twice to create this Loop and we are going to type it again for and we are going to change the first value to be X and the second value to be y and as the count we are going to take object size dot X so this is the size of our object on X and object size dot y so in the simplest case it will be one by one and if the object is larger we are going to calculate the larger values here so we are going to basically get the offset distance we are always assuming that we are placing objects from the bottom left corner if we were to rotate the object we would have to create couple of those for Loops based on their rotation so we are going to add to the return values add and we are going to pass the grid position so this is the original position plus and we are going to add to it a new Vector 3 in and we are going to pass the x value on x0 on Y and the Y value on Z because we are working in the 3D space so we are considering x and z values okay and lastly we need to return the return values the last method that we need to have here is to actually be able to check if we can place object on these positions so we're going to create public ball can place object and we're going to pass the same parameters as we did in the calculated position so let's copy this those parameters and paste them so the grid position and the object size and here we are going to create a list of vector three end positions to occupy and we are going to set it to be calculate positions passing the grid position and the object size and for each of those we need to type 4 each and I'm going to type here VAR pause in our positions to occupy and if any of those are placed objects if this contains key and any of those positions are on the in our dictionary we are going to return false and if everything is all right we are going to return true so let's now go to our placement system script we're going to create here for new values we are going to create a private read data reference and this will be floor data since we have the floor tiles and Furniture data so this is the general objects those are General objects other than the floor objects as you might recall we already have the cell indicator that is a kind of preview for us so we will use it to modify its look based on if we can place objects here or not for this we are going to create a private render and maybe preview render okay and we are going to now go to start method and here we are going to Define floor data equals new and we are going to define the furniture data equals new now we can pass the new or new with the name of the object and we are going to access the preview render equals and we are going to call cell indicator get component in children render and this is because if we go to our prefabs we're going to see that the cursor indicator parent has a child object that actually has the renderer that's why we are accessing it now we need to slide it down to find a place structure method here we are going to move the mouse position and the grid position calculation just below the if check so that we can now perform the check if the placement is valid so we're going to create a ball that you spent validity and we are going to set it to be equal to check basement validity and we are going to pass here the grid position and we can pass also the selected object index just in case although we can access it so right click here and generate this method okay we are going to implement it in a bit but basically if our placement validity is false we're going to return here okay so this is our check so let's copy those lines because we are going to reuse them let's slide it down we are going to check the update method here if you are in the placement mode we are going to move our cell indicator and mouse indicator so we are going to create the space here just after calculating the grid position and we are going to basically do the same let's check the placement validity and if placement validity is false we can do something actually we are going to call the preview render we are going to set the material dot color so the color of the material equals and we are going to call placement validity question mark if the placement validity is true so we can place objects here we are going to set the color dot white and colon we are going to set it only if this is false we are going to set the color to be red and this should give us an indication if we can place object here or not great the last stop is to actually implement this check placement validity so what we are going to do is we are going to Define grided data selected data and we are going to just check if selected index is equals to zero meaning that if it is a floor we are going to add question mark we're going to return floor data hold on and we are going to return the furniture data so for any other ID we are going to return the furniture data if the selected index is zero now actually this is not the ID this is the index so what we could do is we could check the data base dot objects data selected index dot ID just for safety because I have told you that we are going to set the floor ID to be zero so we are checking the ID and we are going to get the selected data based on that obviously if you have more floored objects we are going to have to implement some enum or some other factor to add our data definition okay so now we have the selected data we are going to return selected data dot Check Place can I place object add and we are going to pass the grid position and we are going to pass the database dot objects data passing the selected objects index dot size okay and this is it last thing that I forgot to do is that we need to now go to the place structure method and we need to add the object to the corrected data now here I have mentioned that we need to save those instantiated objects in a list now what we can do is we can go to the top of this plus we are going to create private list of Game objects we are going to add placed game objects and we are going to set it to be a new list okay so what we are going to do is slide it down to the place structure method and you are going to add to this list so place a game objects I have misspelled it we're going to add to this the new game object new object and now we need to add this index of this object to our data so we are going to copy the same code for getting the selected data from our check placement validity and paste it here so we have our selected data and we are going to call selected data dot add object add and we are going to pass the grid position next we are going to pass the object size or database dot objects data selected index select objects index dot size next we need to give the ID so database dot objects data selected object index dot ID and lastly we need to give the index so we are going to assume that we have added the object to the last position so paste game objects not count minus one and for now this will be enough to be able to access this object from our list and in case you want to implement this on your own remove the object from our list and from our map okay let's save this let's go back to ENT and let's press play so now if we select our floor we can place our floor and we can place again and you can hear the sound I have added to inform us that it is the wrong spot to place this and if we select our bed we can place it and we can again hear the sound if we try to place it on any positions that are taken by this bed but we can place it on top of the floor and we can now select the floor and place it below the bed so now it all works and we can select a chair for example and again it cannot be placed where the batteries and now the bed can be placed whenever the chair is taking the position that would be taken by the bed if you were to place it here okay this is great but it would be cool to actually see how big is the bed what is the bed oh what is the bad shape so that we can make a conscious decision if we want to place it here or not so we are going to take care of adding some preview to our game in the next video see you there
Info
Channel: Sunny Valley Studio
Views: 13,915
Rating: undefined out of 5
Keywords: Placement Collision Detection in unity, grid collision detection
Id: ImtOx8n0WLs
Channel Id: undefined
Length: 16min 58sec (1018 seconds)
Published: Tue Mar 28 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.