- Home /
RTS Style pointer with multiple Collider 2D
Hello there!
As of now, I can't find a way of selecting a ingle unit in my rts styled game precisly.
The thing is, all units have there normal non-trigger colliders for close combat, and, in a child object, an trigger collider to detect if enemies are in range for, well... ranged combat.
Alas, I just can't get the mouse to ignore the triggercollider, even so I only want to select a unit if I hit the close combat collider (borders of the associated sprite). Be it with RayCast2D or with on MouseOver, whatever I do, I just cant get the Engine to seperate the TriggerCollider and the normal one.
Any ideas, anyone?
`We rock! Max
Two options:
Use different layers for both colliders, and pass a Layer$$anonymous$$asl to the Raycast.
Use RaycastAll and cycle through the array of hits. Ignore those colliders that are set to IsTrigger = true.
Answer by Fair-Strides · Aug 12, 2016 at 04:22 PM
If it would work out in your design, I recommend putting the long-range and short-range colliders on separate layers. Only one would need to be on a different layer.
At that point, you should be able to use a RayCast2D:
http://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html
"public static RaycastHit2D Raycast(Vector2 origin, Vector2 direction, float distance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity); "
and
"This function returns a RaycastHit object with a reference to the collider that is hit by the ray (the collider property of the result will be NULL if nothing was hit). The layerMask can be used to detect objects selectively only on certain layers (this allows you to apply the detection only to enemy characters, for example)."
Your answer
Follow this Question
Related Questions
Event Trigger Pointer Exit sometimes not recognized 0 Answers
2D RTS unit selection not working 0 Answers
Cursor Toggle in Menu (UI) 1 Answer