I’m working on a tower defense project, but I’m encountering issues with placing the turrets. More specifically, not allowing turrets to occupy the same space. Below are the options I’ve looked at, none of which seems to be a complete solution:
Sphere collider on tower with radius = range. When a creep hits it, it registers them in a list/dictionary and removes them on the corresponding exit event. The tower is kept at a world distance of 10 above the ground plane. The problem here is that towers can be close together so I would need to get ALL colliders it’s inside of and then check the dimensions of their mesh filters as well as the center point and compare them to the range covered by the tower I want to place.
Creeps register when they enter the board, and unregister when they leave it for whatever reason. Each time a tower needs a new target, it checks the distance to all creeps registered. If one is in range, it notes how far it’s travelled and only holds onto the one that’s gone the farthest. With just a regular collider fitting the object, it may be possible to detect the collisions easier, but targeting would be slower.
Raising the height of the tower to be placed above the maximum height any tower will be able to shoot and adding a placement plane that also follows the mouse. The plane would be set to the size of the tower and checks for collisions would go against the plane instead of the tower. The problem here is more with the visuals. The higher the tower is placed, the more of the screen it obscures. In fact, it’s highly likely the placement plane will be hidden by the tower itself!
Turn off all tower colliders when button is clicked to place tower and use a 5-point raycast(center and 4 corners). This would require moving the tower high enough as a raycast from inside a collider doesn’t seem to hit that collider even if it is another object. The problem here is that every placement it would have to search for a list of the towers, turn off their colliders, do the raycast, and then turn them all back on again. I’m not sure what the computational overhead would be like on this option, but it may be the lesser of the evils.
Any refinements to the above methodologies or another one entirely would be greatly appreciated. Who would have thought that of all the things going into this, the one that would hang me up is keeping the towers from being on top of each other?
Actually, that’s not the case. I did some additional test, including taking the positions of the objects instead of the other functions:
And adding a check to see what gameObject.collider.bounds returns:
Definitely puzzling behavior. I’m now more curious than anything else about this behavior. These experiments have shown me a workaround that will also give me greater flexibility on how high above the terrain I want an unplaced tower to hover by simply finding the minimum and maximum x and z values and checking if the min or max of one box is within the min-max range of another and disregarding the y entirely.
Still, it might be useful to know why Bounds doesn’t actually seem to operate properly in C#.