- Home /
AI Picking New Target After First Target Gets Destroyed
Hi! All I would like to know is how I could make my friendly AI pick a new target after it's original target was destroyed. When it's original target gets destroyed, the friendly AI's target comes up as "missing." I have been trying to target a new target (also the enemy) with gameobject.findwithtag but it's not working. So far all I have is when the enemy dies, it tells the friendly AIs that it's destroyed and they need to find a new target. Thanks!! P.S. - I'm trying to target the same enemy kind but different gameobject. Update with code of my friendly unit :P)
var target : Transform; //the enemy's target
var moveSpeed = 4; //move speed
var rotationSpeed = 3; //speed of turning
private var nextFireTime : float;
var fireDelay : float;
public var chasing = false; //Player
var shootSound : AudioClip;
var Dead : GameObject;
var hitPoints : int = 200;
var Bullet : GameObject;
var hitParticles : ParticleEmitter; //Sparks
var hitParticles2 : ParticleEmitter; //Blood
var hitParticles3 : ParticleEmitter; //Dirt
var force = 50.0;
var range = 150.0;
var decalHitWall : GameObject;
var BulletTracer : GameObject;
var SpawnPoint : GameObject;
private var floatInFrontOfWall : float = 0.001;
var damagechest = Random.Range(10.0, 15.0);
var hitdamage = Random.Range(70.0, 80.0);
var myTransform : Transform; //current transform data of this enemy
public var Fire : int = 0;
var hitTime : float;
private var nextHitTime : float;
public var AtPosition = false;
public var JustGotShot = false;
public var targetdestroyed : boolean;
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
function Start()
{
target = GameObject.FindWithTag("Enemy").transform;
}
function Update () {
if (hitPoints <= 0)
{
Destroy(gameObject);
Instantiate(Dead, transform.position, Quaternion.identity);
GameObject.Find("LZ").GetComponent(PointManager).points -= 100;
}
// check distance to target every frame:
var distance = (target.position - myTransform.position).magnitude;
if(JustGotShot == true) {
GetComponent(FriendlyDodge).GetHim = true;
}
if (chasing) {
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;;
if (distance <= 10 && Fire == 1) {
moveSpeed = -1;
}
if (distance >= 11) {
moveSpeed = 4;
}
if(distance <= 4) {
GetComponent(FriendlyRaycast).noMelee = 1;
Fire = 0;
if(Time.time > nextHitTime) {
hitdamage = Random.Range(70.0, 80.0);
GameObject.FindWithTag("Enemy").GetComponent(Enemy).TakeDamage(hitdamage);
nextHitTime = Time.time + hitTime;
}
}
if(distance >= 5) {
GetComponent(FriendlyRaycast).noMelee = 0;
}
}
if(chasing == false && GetComponent(FriendlyDodge).Attack == true && AtPosition == false) {
distance3 = (GetComponent(FriendlyRaycast).LastPlayerPosition - myTransform.position).magnitude;
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(GetComponent(FriendlyRaycast).LastPlayerPosition - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;;
if(distance3 <= 0.5) {
AtPosition = true;
chasing = true;
GetComponent(FriendlyDodge).Attack = true;
}
}
if(GetComponent(FriendlyRaycast).CantSee == true && AtPosition == true) {
chasing = false;
GetComponent(FriendlyDodge).Attack = false;
}
if(Time.time > nextFireTime && Fire == 1) {
FireOneShot();
nextFireTime = Time.time + fireDelay;
audio.PlayOneShot(shootSound);
}
if(targetdestroyed == true) {
FindNewTarget();
}
}
function FindNewTarget() {
target = GameObject.FindWithTag("Enemy").transform;
}
We can be more helpful if you post your code. GameObject.FindWithTag() should work.
Updated..If you need some of the other code, just let me know :) Thanks.
Yes, all the enemy AI's have the tag "Enemy" and their chest has the tag "chest" for the friendly AI to detect it by raycast