- Home /
freindly ai attack other ai.
Hi i made a simple ai that shoot only the player. and i was wondering how to make a Ai that actually shoot other ai's (like a huge ai battle). I'm using java, and i know we need to use an array. but i can't see what to do for making the ai attack other ai's. is there a On tags function maybe? i don't really know. can someone help please.
How do you currently make your ai attack the player? As you have achieved that, what stops you from making it shoot something else than the player?
Answer by seb-lopez · Jan 15, 2017 at 12:22 AM
it's for building friendly ai's to shoot the enemies ai's instead targetting me. i want to make an ai like a sand box. i want to make a script that makes me juste to put multiple ai's in a team and automatically shoot's the other team. a bit like Arma editor where you just place infentries set there teams and see the action taking place. but the problem is that i remarque that there is actually no tutorial explaining that. i know that it's array with loops but i don't know a way to implement that.
here is a turret script for the ai :
//var player : GameObject[];
var player : GameObject;
var safeDist : float = 15;
var currentDist : float;
var shooting : boolean = false;
var bang : AudioClip;
var bulletFab : Rigidbody;
var gunTip : Transform;
var power : float = 1000;
var shotDelay : float = 1;
function Start (){
player = GameObject.FindGameObjectsWithTag("blueTeam");
}
function Update () {
currentDist = Vector3.Distance(transform.position, player.position);
if(currentDist < safeDist){
transform.LookAt(transform.position, player.position);
if(!shooting) shootStuff();
}
}
function shootStuff(){
shooting = true;
//for ( var i = 0 ; i <player.Length; i++){
AudioSource.PlayClipAtPoint(bang, transform.position);
var fwd = transform.TransformDirection(Vector3.forward);
var bulletShot : Rigidbody = Instantiate(bulletFab, gunTip.position, gunTip.rotation);
bulletShot.AddForce(fwd * power);
yield WaitForSeconds(shotDelay);
shooting = false;
// }
}
what i put in "//" is where i think to put. the problem is that i think that the script was meant for one target.
Answer by GarretLawrence · Jan 16, 2017 at 07:42 AM
Say each team has 3 AIs : (A1, A2, A3) vs (B1, B2, B3).
Say after getting list of B team, A1 focus fire on B2, only when B2 die (no matter killed by who), A1 will search for other target until no target left. Is that what you want?
If so you should do something like this
var player : GameObject[];
var currentTarget : Transform;
var safeDist : float = 15;
var currentDist : float;
var shooting : boolean = false;
var bang : AudioClip;
var bulletFab : Rigidbody;
var gunTip : Transform;
var power : float = 1000;
var shotDelay : float = 1;
function Start (){
}
function Update () {
//if target is alive/not null then shoot
if(currentTarget)
{
currentDist = Vector3.Distance(transform.position, currentTarget.position);
if(currentDist < safeDist){
transform.LookAt(transform.position, currentTarget.position);
if(!shooting) shootStuff();
}
}
else //if not then search for new target
{
player = GameObject.FindGameObjectsWithTag("blueTeam");
//pick random target
if(player.Length > 0)
{
Random rand = new Random();
var n = rand.nextInt(player.Length);
currentTarget = player[n];
}
}
}
function shootStuff(){
shooting = true;
AudioSource.PlayClipAtPoint(bang, transform.position);
var fwd = transform.TransformDirection(Vector3.forward);
var bulletShot : Rigidbody = Instantiate(bulletFab, gunTip.position, gunTip.rotation);
bulletShot.AddForce(fwd * power);
yield WaitForSeconds(shotDelay);
shooting = false;
}
even known that this script seems interesting( there is a problem in line 35 with "Random rand" it says that there is no ";" at the end). But i'm going to be more precise by telling like a codition :
is a ai that has a liste of target (in array ). if the target is tag == blue $$anonymous$$m. and if the ai is near of one of these targets. it will shoot them.
and vise versa
@seb-lopez well then this script does exactly what you want... I'm not sure about how Random method declared in Javascript so maybe i was wrong. You can edit the code to :
player = GameObject.FindGameObjectsWithTag("blueTeam");
//pick 1st target from the list
if(player.Length > 0)
{
currentTarget = player[0];
}
it 's saids :
i never been soo close to achieved this script =) but unfortunately it tells me : BCE0022: Cannot convert 'UnityEngine.GameObject' to 'UnityEngine.Transform'.
Oh sorry it should be : currentTarget = player[0].transform;
Your answer
Follow this Question
Related Questions
AI passes through walls and flies 2 Answers
My Eneny AI Script Wont Work? 2 Answers
What am I doing wrong with my AI? 1 Answer
How do I make an enemy lead his shots? 2 Answers
enemy ai movement. 1 Answer