Placement Collision Detection - Grid Placement System in Unity P4

Поділитися
Вставка
  • Опубліковано 28 вер 2024

КОМЕНТАРІ • 48

  • @dashfoxgames
    @dashfoxgames Рік тому +2

    Awesome series and starter kit, thank you! When are the next parts coming? Keen to see how you tackle the preview renders!

    • @dashfoxgames
      @dashfoxgames Рік тому

      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.

    • @SunnyValleyStudio
      @SunnyValleyStudio  Рік тому

      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!

    • @Ghost-wv4ib
      @Ghost-wv4ib Рік тому

      @@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?

  • @stylie473joker5
    @stylie473joker5 3 місяці тому +2

    What if instead of writing and checking the grid data you do raycast checks into the corresponding tiles would that work or could fail

    • @SunnyValleyStudio
      @SunnyValleyStudio  3 місяці тому

      Raycast checks are useful when the placement system is continuous. Its more efficient to query the data if your system is grid based

  • @eugene5717
    @eugene5717 Рік тому +1

    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?

    • @SunnyValleyStudio
      @SunnyValleyStudio  Рік тому +1

      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.

  • @ANDREJJGaming
    @ANDREJJGaming 6 місяців тому +1

    Can someone tell me what kind of plugin in visual studio he is using for auto recommend and auto finishing lines recommendations?

    • @SunnyValleyStudio
      @SunnyValleyStudio  6 місяців тому +1

      Its just intelligence from Visual Studio learn.microsoft.com/en-us/visualstudio/ide/ai-assisted-development-visual-studio?view=vs-2022

  • @Dominisify
    @Dominisify 5 місяців тому +1

    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

    • @SunnyValleyStudio
      @SunnyValleyStudio  5 місяців тому

      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

    • @tharkyawkyaw6844
      @tharkyawkyaw6844 28 днів тому

      Did you find solution? I am stuck at that too.

  • @relaxingguy2
    @relaxingguy2 Рік тому

    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?

    • @SunnyValleyStudio
      @SunnyValleyStudio  Рік тому

      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)

  • @Hezekiel112
    @Hezekiel112 4 місяці тому

    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 ?

    • @SunnyValleyStudio
      @SunnyValleyStudio  4 місяці тому +1

      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...

    • @Hezekiel112
      @Hezekiel112 4 місяці тому

      @@SunnyValleyStudio thanks i'll go looking at these stuff. keep doing these nice tutorials they're really helpfull !

  • @Sussy_Boy625
    @Sussy_Boy625 6 місяців тому +1

    I spent 3 nights debugging a error only to realise I forgot to capitalise start() 💀

    • @SunnyValleyStudio
      @SunnyValleyStudio  6 місяців тому

      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! 👏

  • @huseyinss413
    @huseyinss413 Рік тому +3

    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 :)

    • @SunnyValleyStudio
      @SunnyValleyStudio  Рік тому

      Good to hear that you were able to solve it! Sorry for not implementing a check to avoid such a bug

    • @huseyinss413
      @huseyinss413 Рік тому

      @@SunnyValleyStudio My mistake :) Thanks for the tutorial :)

    • @huseyinss413
      @huseyinss413 11 місяців тому

      ​@@SunnyValleyStudio Hi :) How can I save the locations of objects added in the editor or in runtime?

  • @RomekjeG
    @RomekjeG 10 місяців тому +2

    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?

    • @SunnyValleyStudio
      @SunnyValleyStudio  10 місяців тому +1

      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.

  • @afabulouswhale
    @afabulouswhale Рік тому +1

    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

    • @SunnyValleyStudio
      @SunnyValleyStudio  Рік тому +1

      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.

  • @mira3504
    @mira3504 4 місяці тому +1

    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.

    • @SunnyValleyStudio
      @SunnyValleyStudio  4 місяці тому

      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

  • @vvbgamedevx11
    @vvbgamedevx11 6 місяців тому +2

    Thanks for creating such an awesome free tutorial.

  • @captainnoyaux
    @captainnoyaux Рік тому +3

    Cool video thanks !

  • @hunneebunn9593
    @hunneebunn9593 10 місяців тому +1

    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?

    • @SunnyValleyStudio
      @SunnyValleyStudio  10 місяців тому +1

      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!

    • @benjioffdsv
      @benjioffdsv Місяць тому

      @@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

  • @BunPatty
    @BunPatty Рік тому +1

    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 ?

    • @SunnyValleyStudio
      @SunnyValleyStudio  Рік тому

      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.

    • @BunPatty
      @BunPatty Рік тому

      @@SunnyValleyStudio Thanks for the help.

  • @Кролти
    @Кролти 2 місяці тому

    please tell me how to make the obstacles detection and placing buildings in one specific place. I don't understand how to code it

  • @사쿠노
    @사쿠노 10 місяців тому +1

    감사합니다 ( thx)

    • @SunnyValleyStudio
      @SunnyValleyStudio  10 місяців тому +1

      I'm glad that you are enjoying this series 🙂

  • @_Enj0y_
    @_Enj0y_ Рік тому +5

    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!👍

    • @SunnyValleyStudio
      @SunnyValleyStudio  Рік тому

      Awesome, thank you! 👍

    • @erikm9768
      @erikm9768 10 місяців тому

      You really needed to look at a video to figure this out?

  • @XMolha
    @XMolha 4 місяці тому

    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.

    • @SunnyValleyStudio
      @SunnyValleyStudio  4 місяці тому

      So you have found a solution? If you have any more questions feel free to ask 🙂