Enemy Targetting Without Stacking "Turret" Objects

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:

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

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

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

  4. 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? :stuck_out_tongue:

So you’re just trying to prevent putting two turrets in the same place?

First off drop all references to creeps - you don’t care what the creeps are doing, you just want the tower information.

Option one is easy - Mesh has a .bounds property.

Build a new Bounds where you want your new turret to be placed, then for each existing turret:

if ( turret.meshFilter.mesh.bounds.Intersects( newTurretBounds ) ) canPlaceTurret = false;

I completely forgot about bounds.Intersects! Sheesh. I knew the last couple of days have been rough on me, but still :stuck_out_tongue: Thanks, Loius.

Finally had time to do this, but the results are not what I expected. In the tower script itself, this function does the check:

	public bool TestBounding (Bounds newTower)
	{
		Debug.Log(newTower + ", " + gameObject.GetComponent<MeshFilter>().mesh.bounds);
		if(gameObject.GetComponent<MeshFilter>().mesh.bounds.Intersects(newTower))
			return false;
		else
			return true;
	}

And in the interface code:

			if(Input.GetMouseButtonDown(0)  towerSelected != null)
			{
				// check bounding box of meshes for intersection
				bool placeable = true;
				foreach(GameObject go in towersPlaced)
				{
					if(!(go.GetComponent<Tower>().TestBounding(towerSelected.GetComponent<MeshFilter>().mesh.bounds)))
					{
						Debug.Log ("Intersects!");
						placeable = false;
						break;
					}
				}
				
				if(placeable)
				{
					towersPlaced.Add(towerSelected);
					towerSelected.GetComponent<Tower>().Activate();
					towerSelected = null;
				}
			}

Works fine until the first tower is placed. After that, it ALWAYS says it intersects, even if I’m way outside even the colliders on the tower.

I’m guessing the Center is the offset rather than the actual coordinates as neither tower is anywhere near the origin.

A second look has me thinking it’s actually giving back the sphere collider’s information, as the tower in question’s size in inspector is (20,40,20).

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