im making a tower for a tower defense game. my root object is called tower_obj and it has a child object that holds the mesh.
…towe_obj (sphere trigger)
…mesh (cube collider)
i have created a pretty big sphere trigger to see when enemies are in sight. and on the child object i put a reg box collider so i know when tower is selected with mouse event.
now my issue is when the sphere trigger is disabled i am able to register a mouseup event on the mesh, but as soon as i turn on my sphere trigger on the tower obj , the mesh no longer fires the event.
i tried reversing the rolls, thinking maybe some sort of priority depdning on the child parent relations ship, but same result.
any advice ? should i be looking in the sphere trigger to see if the mouse up event is also inside the mesh collider and force the mouse event myself?
I’m pretty sure MouseUp internally does a raycast, so the raycast is hitting the trigger and returning that.
Several ways around this:
Redo your tower code to not use trigger colliders - simplest way would be to keep a list of enemies on the map (or give enemies the “Enemy” tag and use GameObject.FindWithTag(), but that’s more computationally expensive), and have each turret go through the list, find the nearest enemy (or whatever behavior you want), and target it if it is in range.
Put the tower meshes on a separate physics layer, and make your own “version” of MouseUp by manually raycasting from the mouse pointer (see here - this isn’t exactly what you want to do, but basically replace Plane.Raycast with Physics.Raycast and check to see if you hit a tower) and having the raycast only interact with the towers (using the layermask parameter). BTW it is way, way better to raycast once, and then check the returned object against a list of towers, than to raycast for each tower.