Checking if an endpoint of a raycast lands inside a trigger

Quick question:

I’m working on a Tower Defense game. I’m using a trigger - called “Construction Trigger” - to make sure I don’t build turrets in the paths of creeps.

I have the screen-to-world conversion code for the mouse. What I’m trying to do is detect if the point that’s hit falls inside of the trigger. If it is, a tower can be instantiated, otherwise, I can play an annoying sound - all things I know how to do.

…Just not the part where I check if a point falls inside of a trigger.

Current Mouse->World code:

var TowerPrefab : GameObject;
var distance : int;

function OnMouseUp ()
{
   var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
   var hit : RaycastHit;

   if( Physics.Raycast( ray, hit, distance ) )
   {
     Instantiate( TowerPrefab, hit.point, Quaternion.identity );
   }   
}

Try having an invisible mesh (cube, or something like that) centered on the mouse location, and checking to see if it collides with your triggers.

Probably you’d check bounds.Contains(raycastHit.point).

–Eric

Good points, both. Thanks for the help on that!

I’d also forgotten that OnMouseDown/Up also requires either a GUI element or Collider to be present. Thanks for the help, though!