Okay, Still having problems,
I have a turret on my scene that has been duplicated a few times. All the turret does is looks at an enemy within its radius and shoots at it. The enemy are from an array (they are initiated prefabs). So every enemy that steps into the turrets radius it is added to a Generic.List. Heres what works fine: Enemy is added to the List when they enter radius (collider), and are removed when they move out. But a bug crops up when the turret manages to kill (destroy) the enemy, and some turrets targets are 'missing' due to this, therefore they just stop, and don't do anything?
Watch My Video: www.olliejones.com/TurretHelp2.mov
The turrets target is its current enemy selected on the List, so when that gets destroyed, it doesn't find a new enemy, and I don't know how to code for that not to happen.
I have 2 scripts, one called SmoothLookAt (attached to the turret) and DamageReceiver (attached to enemy prefab). I believe this is happening because one of my variables is a static, but I need this for my DamageReicver to talk to my SmoothLookAt.
Here Are My Scripts:
SmoothLookAt:
static var damping = 6.0;
static var smooth = false;
static var ActivateScorpionRange = false;
static var ScorpionRange : float = 8;
static var Scorpion : boolean = true;
var target : Transform;
var closeObjects = new System.Collections.Generic.List.<Transform>();
static var EnemyFound = false;
//static var LookAtDead = false;
function Update ()
{
//---Movement of Scorpion---\\
if(Scorpion == true)
{
if (smooth) //Smooth Rotation\\ -- Note To Self - damping = lagged fire
{
if(closeObjects.Count == 0)
{
target = null;
return;
}
target = closeObjects[0];
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
BroadcastMessage("Fire");
}
else //Snap Rotation\\
{
//Make sure there is an enemy. \\
if(closeObjects.Count == 0)
{
target = null;
return;
}
target = closeObjects[0];
transform.LookAt(target);
BroadcastMessage("Fire");
//closeObjects.Remove(transform);
}
}
//DamageReceiver uses this to remove target once destroyed \\
if(DamageReceiver.Dead == true) // I BELIEVE THIS MIGHT BE THE PROBLEM - STATIC!
{
closeObjects.Remove(target);
target = null;
DamageReceiver.Dead = false;
return;
}
}
//When an enemy enters the trigger, add them to the list.
function OnTriggerEnter (enemy : Collider)
{
closeObjects.Add(enemy.transform);
}
//When they leave, take them off of the list.
function OnTriggerExit (enemy : Collider)
{
closeObjects.Remove(enemy.transform);
}
DamageReciver:
var hitPoints = 10.0;
var detonationDelay = 0.0;
var explosion : Transform;
var deadReplacement : Rigidbody;
var credits = 100;
static var Dead = false; //this is used to tell SmoothLookAt the enemy has been destroyed - now remove it from list.
function ApplyDamage (damage : float) {
// We already have less than 0 hitpoints, maybe we got killed already?
if (hitPoints <= 0.0)
//credits += 100;
return;
hitPoints -= damage;
if (hitPoints <= 0.0) {
// Start emitting particles
var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
if (emitter)
emitter.emit = true;
Invoke("DelayedDetonate", detonationDelay);
}
}
function DelayedDetonate () {
Dead = false;
BroadcastMessage ("Detonate");
//BroadcastMessage("LookAtDead");
Dead = true;
}
function Detonate () {
// Destroy ourselves
//GetComponent closeObjects.Remove
Destroy(gameObject);
// Create the explosion
if (explosion)
Instantiate (explosion, transform.position, transform.rotation);
// If we have a dead barrel then replace ourselves with it!
if (deadReplacement) {
var dead : Rigidbody = Instantiate(deadReplacement, transform.position, transform.rotation);
// For better effect we assign the same velocity to the exploded barrel
dead.rigidbody.velocity = rigidbody.velocity;
dead.angularVelocity = rigidbody.angularVelocity;
}
// If there is a particle emitter stop emitting and detach so it doesnt get destroyed
// right away
var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
if (emitter) {
emitter.emit = false;
emitter.transform.parent = null;
}
}
Please help me out, this has been bothering me for sometime now, and I can't make any progress with my game until this is fixed.
Thanks, Ollie