Advice for Detecting Intersections With Object Being Placed

I’m working on a tower defense game and rather than limiting the player to only placing towers on specific, predefined spots, I want to let him place them anywhere on the ground that is open. However, towers should not intersect each other or other world objects such as buildings or walls.

I’m wondering what the best way to implement this would be. I have a script on my camera that does a ray cast every frame to get the point on the terrain under the cursor, that’s simple enough. Now I want to know if the selected tower type will fit there, or if there is another obstacle nearby.

Should I attach a script to the tower that’s being placed on the ground, and in that script have an OnCollisionEnter() callback that simply tells the camera that the tower has collided with something and can’t be placed there? Or is there a way I can do a “cube cast”, kind of like how I can do a Physics.Raycast or Physics.SphereCast and handle the collision detection in the same script on the camera that I check for the ground point under the cursor?

then use the collider on your prefab to disable placement ability and activate your red outline… you can have any function you want with a trigger on your prefab - plus adjust the size of the collider so it gives you the right buffer zone etc…

function OnTriggerEnter (other : Collider) {
    Destroy(other.gameObject);
    //or whatever else you want to do
}

if your prefab has a collider then maybe you could just have it destroy itself if it is touching anything like a building or tree etc (use tags)… It would look like nothing happened but you can instantiate a GUI label (for a couple seconds) that says “try again” etc…

That way you could only place a tower when it is not touching anything else. Also the only code you have would be running on your prefab and not running in your update on your camera…