- Home /
RTS Object selection Ignoring Colliders
I'm working on a sort of RTS where you'll be able to select object (character) and click around to move it. To do this I'm casting a Ray from mouse if the hit object have the interactive class, I'll consider it selected.
Now the problem is 'cause I need to add second collider which is pretty whide 'cause is used for attack range.
Obviously the ray hit this collider ignoring all other units inside of his radius.... and I'm stuck :\
That's why I need to ignore the shperecollider but not the box collider.... advice?
Answer by FortisVenaliter · May 03, 2016 at 07:55 PM
Well, one way would be to utilize the physics layers, found in Edit->Project Settings->Physics. You can customize which objects collide with others, and specify a layer on your raycast.
The other option, which I'd highly recommend, would be to use a simple script that stores the attack radius as a float, rather than as a sphere bounding volume, and do that math manually, since the sphere isn't a real collider.
You mean to made a list with all enemies and attack the inside the range, ins$$anonymous$$d using a collider. not exalt me, but I guess it's the easiest way to achieve the same result.
$$anonymous$$aybe I could create an Empty Object as Child, and place there Sphere Collider, than mede the child-object as IgnoreRaycast
Huston, can u help me with "do that math manually" ? :)
It would be something like:
bool inRange = (my.transform.position-other.transform.position).magnitude < attackRadius;
so, in Update (or FixedUpdate) I should check with a foreach(unit in EnemyList) if the are in range?
isn't very CPU expensive ?
AT$$anonymous$$ i've achieved by adding a RigidBody (use gravity uncheck) to the object with the SpehereCollider which sit on layer Ignore Raycast. On this way, the sphere does not interfere with mouse raycast, and with a method which sit on the child object I pass the colliders to the parent.
Answer by lactus · May 04, 2016 at 06:30 PM
don't use physics for selecting more than one unit.
Back in my old wild days I used Physics.OverlapSphere() to select units by dragging GUI.Box()es over them. It was incredibly slow. A better way is using a List of selectable Entities und check if their position in screenSpace (Camera.main.WorldToScreenPoint(obj.transform.position)) is in my selection-rect.
Yes, but $$anonymous$$e is an RTS and they should fight also when are off-screen.