- Home /
RTS range detection
I have an RTS game with a few units called tank and a hostile enemy.
How would I go about detecting when an enemy is within range of one of my units so they can turn and fire on it.
JavaScript please
Answer by ninjaboynaru · Mar 25, 2013 at 10:38 PM
Finally after 2 hours of trying I got it. Physics.SphereCat solved the problem.
var hits : RaycastHit;
if(Physics.SphereCast(transform.position, 50, transform.forward, hits, 0.1)){
Debug.Log(hits.collider.tag);
}
Now I just need to add the enemy to an array and figure out how to get the tank to pick which enemy and attack it, not to mention clearing the array when the enemy is no longer in the array.
Thanks for all the help. Though jmatthews or vagos answers did not solve this problem they will be helpfull when i get to creating other parts of the game.
Answer by jmatthews · Mar 25, 2013 at 05:50 PM
I can tell you conceptually and with some pseudo code...
You'll want each of your tank(game objects) to have a sphere collider which is marked as a Trigger. The you'll want a method that looks similar to this:
void OnTriggerEnter(Collider other) { if(other.tag == "enemy") { target = other.transform; }
}
where each potential enemy has an "enemy" tag in the editor, and target is a transform that you'll use to aim the tank's weapons with.
One more detail, in order for the TriggerEvent to fire one of the two objects must have a rigid body attached to it.
Tried but not possible as I need to be able to click on the tanks original collider to add it to the selectedUnits que
Have a second collider on a child object that is on a different layer...
Answer by vagos21 · Mar 25, 2013 at 05:57 PM
the way i do it for my tower defense game is to use sphere colliders with the range i want them, since they're faster than any other collider primitive. all you have to do is 1. add sphere collider to your units (set it to trigger) 2. add sphere collider to enemy units (set it to trigger) 3. add a rigidbody to either your unit or the enemy, and set it to kinematic (needed for the collision events to work) 4. create a tag for enemy units, for example called "enemyunit" and assign it to all your enemy units 5. assign a script like this one to your units:
private var goUnderAttack:GameObject;
function OnTriggerStay(other:Collider) {
if (other.tag == "enemyunit"){ //lock on enemies only
goUnderAttack = other.gameObject;
Debug.Log(gameObject.name + " is locked on " + goUnderAttack.name);
}
}
goUnderAttack will be the gameobject of your enemy that the tower is locked on. then you can do anything you want with it. i use OnTriggerStay instead of OnTriggerEnter, because if other enemy units enter the range and your current locked on enemy dies, they will be the next to attack. if you use OnTriggerEnter, other enemies might enter the range but will not be attacked. i hope this was helpful enough!
sorry the answer was given while i was typing in my own... :D this is interesting to see others work the same way, and i'd also love to know the solution to this :) even though, i think i'd go away with an empty GO that would update its position on every frame to be the same as the unit's position, and set the sphere collider on that "helper" object. then i'd set a tag of "rangecollider" to that GO and only let mouse interact with my unit and not the "rangecollider" tagged GOs...
Your answer
![](https://koobas.hobune.stream/wayback/20220613095049im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
OnTriggerEnter NullReferenceException 0 Answers
Destroy On Collision will not work with spawned AI 1 Answer
collision script to destroy 2 Answers
two objects colliding 3 Answers
Collide detection with tag [JS] 0 Answers