You mean on the edge of the Grid area? If so you would need to add a limits to your grid and add a if check to verify if the placement positions are within the limit. It is pretty easy to add so I don't plan to implement it in the tutorial and instead want to show how to add the preview and removal mechanic. Sorry about that!
@@SunnyValleyStudio I also encountered this problem, and I was struggling to fix it. How exactly would you limit your grid, and then check if it the preview object is inside the grid borders or outside?
i would like to know why you used the newton convention for the occupiedPosition and Pascal for the id and placeobjectindex, is it because you kept it public and set accessibility keywords for the other variables?
Probably no particular reason beside me not spotting the difference. In general I use PascalCase for method names and CamelCase for fields (currently I am creating a habit for placing _ in the names of private fields). Still sometimes to save time I keep a field public instead of adding [SerializeField] and making it private.
Hello, can you suggest how to fix bug where if you place a 2x2 object on last tile to the right it goes outside grid? 1x1 stays within grid but it's not been mentioned that I can see
Do you mean that it lets you place 2x2 even though there is a single tile available? In that case you have to define a new class called "GridSize" where you would specify the grid size and have a method called "bool VerifyPlacement(Vector3Int gridPos)" where you pass the placement position. For 2x2 you would send there all 4 positions. If the result is false you would make the Placement Validation fail. So you would integrate this into our placement validation logic
You want to adjust the grid to prefab size. You can do that by modifying the "Grid" component and I think the material (in the shader settings we had the size)
I guess that the optimisation is very done poorly, you are reading trough a list of vector3 each frame ... giving me 1.1ko of GC, is there another way of doing this ?
You can cache the result and do that every X frames or X seconds. You could use Jobs system to optimize it. It really depends on the platform that you are developing...
Don't worry about it 👍 It might seem like a waste of time but later down the road this experience will save you much more time. I know. I did those mistakes already 🙂 Good job figuring it out on your own! 👏
Detection not work :( I got all the code from your github account, I watched all the videos completely. I set up the scene. I can create and switch between grids, but I cannot prevent placing two objects on top of each other. I don't get a red warning when I hover over a filled grid and I can create Problem is solved :) my SO objectSize 0x0 :) change 1x1 :)
Maybe a simple solution, but I want to use this for a game like Clash of Clans. This would work like a charm for grid positions, but what about environment? How can I create a map, then place the grid on top and check if the building (furniture) is not colliding with a tree or something?
when we have our floorData we add to it positions where we have placed the floor elements. We could do the same for trees, or just obstacles. How? Well you can place obstacles procedurally and simply add them or raycast at each square from the sky and detect the obstacles and add them. When we have that all you need is to ensure that when placing structures you always check if the selected positions are not present in our ObstacleData structure. I think we had something like "CanIPlaceHere" method so you would just add the check to it.
I'm having trouble right now, I'm trying to make soil that seeds can only be placed on but also furniture too using this sytem and Im not sure how to do this
Well there are 2 ways. We have something like "GridData" which contains an collection of Vector3Int representing occupied positions. For the floor I think we have called it "floor". If you want to place either seeds or floor you use the same data and store a different index in the stored data. If you can place floor over the seeds you can have a check during placement (I think we have "CanIPlaceHere" method). YOu would check "if seedsData has position X inside remove it and add it to floorData". You need to think from the perspective of data to solve those kinds of problems.
Great video! Though I have a question for this project I wanna work on; would the implementation be similar for building vertical walls and want to be able to hang items on the walls? Say clocks, paintings, etc.
I believe that you could have a new grid for the wall objects. That being said I like the idea where we use the same grid but check "if there is a wall nearby and it cases the 'placement direction' ? ". This way we can stick with our original grid and just add a new GridPlacementData (the structure that stores the objects for different placement modes). I hope that it makes sense! PS. Obviously this would work if your objects that you hang are also 1x1 or 1x2. Otherwise you would have to reuse the same setup and simply create a new grid for placement of smaller objects
so, i have my prefabs offset to the center of the tile, but if i have a big prefab, it only checks to see if that single tile is occupied instead of checking for collision. how can i fix this?
I think that we have set in the Data in the Scriptable object the "size" parameter for each object. That is what makes the algorithm check the tiles around the start point. The prefabs size doesn't matter - we adjust its position and size so it fits the type of placement (from the bottom-left corner) as we are using in our project. I hope this helps!
@@SunnyValleyStudio so the problem in case you wanna fix it was that in your code you swapped "returnVal.Add(gridPosition + new Vector3Int(x, y, 0));" with "returnVal.Add(gridPosition + new Vector3Int(x, 0, y));" (you forgot it was in grid coordinates so z should be y). This is both in the video and the GitHub and took me quite a while to find the issue
You create separate Data (Dictionary / collection - in our case we have GridData) for the Floor placement, big object placement, and OnObjectPlacement. When you add say a "book" you can now check "is there "big item" ? If yes - forbid placement. If No ask "is there already this position in the "OnObjectPlacement" data? If yes Forbid placement. If no place the book and add it to OnObjectPlacement data. When you have furniture you add another data and modify your book placement logic to place the book "on top" of the furniure (for example by making sure that the furniture has a collider and you can raycast from just below the ceiling towards the ground.
7:42 (gridposition + new Vector3Int (x, y, 0)) - This line of code works perfectly for my project! 🎇🎇 Subscribed to your channel, looking forward to more awesome content!👍
Hey There. First of all, thanks for the awesome video. I am having trouble after adding a prefab to the grid. It adds extra "slots" to the grid to adjacent squares. For example, I'm setting a 1by4 object, and it sets the object from the original position, 3 to one side and 3 to the opposite side. The big bed is adding an additional line to the opposite side as well. I checked both, and they are with the correct size in the DataBase (1by4 and 2by2). Do you have any idea about what this bug might be? Thanks in advance. Never mind that. I understood what was happening. I won't let me put something that won't fit. Thankssss.
Awesome series and starter kit, thank you! When are the next parts coming? Keen to see how you tackle the preview renders!
Also i noticed an issue that you will likely fix, but any object bigger than 1x will be allowed to place on the edge causing an overhang.
You mean on the edge of the Grid area? If so you would need to add a limits to your grid and add a if check to verify if the placement positions are within the limit. It is pretty easy to add so I don't plan to implement it in the tutorial and instead want to show how to add the preview and removal mechanic. Sorry about that!
@@SunnyValleyStudio I also encountered this problem, and I was struggling to fix it. How exactly would you limit your grid, and then check if it the preview object is inside the grid borders or outside?
What if instead of writing and checking the grid data you do raycast checks into the corresponding tiles would that work or could fail
Raycast checks are useful when the placement system is continuous. Its more efficient to query the data if your system is grid based
i would like to know why you used the newton convention for the occupiedPosition and Pascal for the id and placeobjectindex, is it because you kept it public and set accessibility keywords for the other variables?
Probably no particular reason beside me not spotting the difference. In general I use PascalCase for method names and CamelCase for fields (currently I am creating a habit for placing _ in the names of private fields). Still sometimes to save time I keep a field public instead of adding [SerializeField] and making it private.
Can someone tell me what kind of plugin in visual studio he is using for auto recommend and auto finishing lines recommendations?
Its just intelligence from Visual Studio learn.microsoft.com/en-us/visualstudio/ide/ai-assisted-development-visual-studio?view=vs-2022
Hello, can you suggest how to fix bug where if you place a 2x2 object on last tile to the right it goes outside grid? 1x1 stays within grid but it's not been mentioned that I can see
Do you mean that it lets you place 2x2 even though there is a single tile available? In that case you have to define a new class called "GridSize" where you would specify the grid size and have a method called "bool VerifyPlacement(Vector3Int gridPos)" where you pass the placement position. For 2x2 you would send there all 4 positions. If the result is false you would make the Placement Validation fail. So you would integrate this into our placement validation logic
Did you find solution? I am stuck at that too.
Sir I would like to ask how about changing the prefabs, do i have to increase the grid to match the size of my prefabs?
You want to adjust the grid to prefab size. You can do that by modifying the "Grid" component and I think the material (in the shader settings we had the size)
I guess that the optimisation is very done poorly, you are reading trough a list of vector3 each frame ... giving me 1.1ko of GC, is there another way of doing this ?
You can cache the result and do that every X frames or X seconds. You could use Jobs system to optimize it. It really depends on the platform that you are developing...
@@SunnyValleyStudio thanks i'll go looking at these stuff. keep doing these nice tutorials they're really helpfull !
I spent 3 nights debugging a error only to realise I forgot to capitalise start() 💀
Don't worry about it 👍 It might seem like a waste of time but later down the road this experience will save you much more time. I know. I did those mistakes already 🙂
Good job figuring it out on your own! 👏
Detection not work :( I got all the code from your github account, I watched all the videos completely. I set up the scene. I can create and switch between grids, but I cannot prevent placing two objects on top of each other. I don't get a red warning when I hover over a filled grid and I can create
Problem is solved :) my SO objectSize 0x0 :) change 1x1 :)
Good to hear that you were able to solve it! Sorry for not implementing a check to avoid such a bug
@@SunnyValleyStudio My mistake :) Thanks for the tutorial :)
@@SunnyValleyStudio Hi :) How can I save the locations of objects added in the editor or in runtime?
Maybe a simple solution, but I want to use this for a game like Clash of Clans. This would work like a charm for grid positions, but what about environment? How can I create a map, then place the grid on top and check if the building (furniture) is not colliding with a tree or something?
when we have our floorData we add to it positions where we have placed the floor elements. We could do the same for trees, or just obstacles. How? Well you can place obstacles procedurally and simply add them or raycast at each square from the sky and detect the obstacles and add them.
When we have that all you need is to ensure that when placing structures you always check if the selected positions are not present in our ObstacleData structure. I think we had something like "CanIPlaceHere" method so you would just add the check to it.
I'm having trouble right now, I'm trying to make soil that seeds can only be placed on but also furniture too using this sytem and Im not sure how to do this
Well there are 2 ways. We have something like "GridData" which contains an collection of Vector3Int representing occupied positions. For the floor I think we have called it "floor". If you want to place either seeds or floor you use the same data and store a different index in the stored data. If you can place floor over the seeds you can have a check during placement (I think we have "CanIPlaceHere" method). YOu would check "if seedsData has position X inside remove it and add it to floorData".
You need to think from the perspective of data to solve those kinds of problems.
Great video! Though I have a question for this project I wanna work on; would the implementation be similar for building vertical walls and want to be able to hang items on the walls? Say clocks, paintings, etc.
I believe that you could have a new grid for the wall objects. That being said I like the idea where we use the same grid but check "if there is a wall nearby and it cases the 'placement direction' ? ". This way we can stick with our original grid and just add a new GridPlacementData (the structure that stores the objects for different placement modes). I hope that it makes sense!
PS. Obviously this would work if your objects that you hang are also 1x1 or 1x2. Otherwise you would have to reuse the same setup and simply create a new grid for placement of smaller objects
Thanks for creating such an awesome free tutorial.
Thanks for watching 🙂
Cool video thanks !
Thanks for watching 🙂
so, i have my prefabs offset to the center of the tile, but if i have a big prefab, it only checks to see if that single tile is occupied instead of checking for collision. how can i fix this?
I think that we have set in the Data in the Scriptable object the "size" parameter for each object. That is what makes the algorithm check the tiles around the start point. The prefabs size doesn't matter - we adjust its position and size so it fits the type of placement (from the bottom-left corner) as we are using in our project. I hope this helps!
@@SunnyValleyStudio so the problem in case you wanna fix it was that in your code you swapped "returnVal.Add(gridPosition + new Vector3Int(x, y, 0));" with "returnVal.Add(gridPosition + new Vector3Int(x, 0, y));" (you forgot it was in grid coordinates so z should be y).
This is both in the video and the GitHub and took me quite a while to find the issue
If I want to Place objects only On some Specific Objects , like Chair must be Placed only on Floor Tile not on the Ground.. How do I tackle that ?
You create separate Data (Dictionary / collection - in our case we have GridData) for the Floor placement, big object placement, and OnObjectPlacement. When you add say a "book" you can now check "is there "big item" ? If yes - forbid placement. If No ask "is there already this position in the "OnObjectPlacement" data? If yes Forbid placement. If no place the book and add it to OnObjectPlacement data. When you have furniture you add another data and modify your book placement logic to place the book "on top" of the furniure (for example by making sure that the furniture has a collider and you can raycast from just below the ceiling towards the ground.
@@SunnyValleyStudio Thanks for the help.
please tell me how to make the obstacles detection and placing buildings in one specific place. I don't understand how to code it
감사합니다 ( thx)
I'm glad that you are enjoying this series 🙂
7:42 (gridposition + new Vector3Int (x, y, 0)) - This line of code works perfectly for my project! 🎇🎇 Subscribed to your channel, looking forward to more awesome content!👍
Awesome, thank you! 👍
You really needed to look at a video to figure this out?
Hey There. First of all, thanks for the awesome video. I am having trouble after adding a prefab to the grid. It adds extra "slots" to the grid to adjacent squares. For example, I'm setting a 1by4 object, and it sets the object from the original position, 3 to one side and 3 to the opposite side. The big bed is adding an additional line to the opposite side as well. I checked both, and they are with the correct size in the DataBase (1by4 and 2by2). Do you have any idea about what this bug might be? Thanks in advance.
Never mind that. I understood what was happening. I won't let me put something that won't fit. Thankssss.
So you have found a solution? If you have any more questions feel free to ask 🙂