- Home /
Targeting other objects of the same type but not self
hi i am making a space game , the problem is when i make the AI try to target another Ai , it targets itself , all the ships are on the same layer and have the same names.
how i can make them target only other ships , also i have the same problem in collision avoiding.
thx for help
So, why not just check
if(target == this)
{
return;
}
before the rest of your logic?
Answer by aldonaletto · Dec 22, 2011 at 02:34 AM
You should edit your question and post your script. Anyway, to target another object you must get some reference of it - transform, collider, rigidbody, gameObject etc. Supposing you want to find all AI ships with the same tag and select one of them randomly, you could do something like this:
var target: GameObject;
function FindAI(){ var ships = GameObject.FindGameObjectsWithTag("AiShip"); do { target = ships[Random.Range(0, ships.length)]; // get a random element } while (target == transform.gameObject); // repeat if found itself }
Your answer
Follow this Question
Related Questions
What would be the best way to setup Target priority 1 Answer
How to determine a "cone of influence" used for targeting an object. 2 Answers
Use of an AI script, and having it NOT go through walls using a character controller 0 Answers
AI Picking New Target After First Target Gets Destroyed 0 Answers
Hey I need an AI script for a character noticing nearby characters! 2 Answers