Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Runtime Building Placement (on floors/ceilings&walls)

Discussion in 'Scripting' started by Napoleonite, Mar 26, 2015.

  1. Napoleonite

    Napoleonite

    Joined:
    Mar 15, 2015
    Posts:
    27
    Usually when you place buildings in an rts-game, you have some kind of grid that you can place the buildings on and they may snap to the grid and color green or red depending on whether the placement is valid.

    However, I need several grids on several surfaces in a 3rd person game. I need to place buildings on the ceilings, walls and floors. But some walls/ceiling&floors can not be build upon or parts of those may be blocked by other objects.

    I can't think of any decent method (nor find any in the asset store) to make it work. So far the best I could think of would be something like:
    I make an invisible grid-prefab that can be relocated, resized and rotated. Then I place the prefab on the wall and scale it to match the size of the wall. And then (for example) another prefab for the ceiling in that corridor. But if there is a pillar in the wall somewhere, then I must still figure out how to tell the grid that you can't build where that pillar is located. Or just place multiple smaller grid-prefabs on that wall.
    All by all this is gonna be very messy and I may have to add a lot of prefabs to the level. It may not perform well and it can be tedious if the level changes.

    Any ideas or tutorials or links are very appreciated.
     
  2. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,390
    Hey Napoleaonite,

    You're right, it will take a lot of work. But it sounds perfectly doable.

    I'm not too sure on how to make a grid for those situations, although I have some guesses. But I am thinking more-so on your "you can't build a building here" problem. Have you though about using colliders at all? With the updated PhysX in Unity 5, performance should be decent enough, although I don't know exactly what your game needs.

    Are you trying to " X out " or turn red the exact grid squares the obstacle encompasses? If you are, it will be a bit trickier I think. But if you don't need that, you can simply do a "Physics.spherecheck" or some such function to detect if there is a "building collider" nearby, and simply not let the player build in that location (you could also use triggers, you don't necessarily need colliders).

    Essentially your problems are all going to come down to location, location, location (i.e. vector3 transform positions etc).

    Perhaps, if you know the center location of each of the cells on your grid, you could fire a raycast (Physics.raycast) at each grid cells center location to see if there is any collider obstructing that cell. But again, you would need to use colliders on your building gameobjects, and you'd have to worry about "partial" cells, where the building may not quite take up the entire area of the cell -- but it might work well enough (spherical/circular buildings are troublemakers in that regard).

    But once you know if a building (via it's physics collider) is taking up a certain grid space, you can turn that grid cell red/green according to the raycast hit condition.

    I think you're going to have to be working with arrays for your grid cells, and if you use raycasts, you'll probably have to iterate through your grid cell arrays in order to carry out various conditions accordingly.

    Your grid prefab idea sounds like a good start. I would do it, test it, and see what happens.
     
unityunity