I am making a tower defence game, my towers are using collider system to target the enemys but my problem is when a enemy is killed inside the collider(it is a sphere collider) it will not target the next enemy if i move the enemy outside then inside the collider agian it will target, and it keeps when i spawn one with my turret spawn script aiming way to much to the right and it is for all spawned turrets
Here is the script
using UnityEngine;
using System.Collections;
public class Ground_Turret : MonoBehaviour {
public GameObject myProjectile;
public float reloadTime = 1f;
public float turnSpeed = 5f;
public float firePauseTime = .25f;
public float errorAmount = .001f;
public Transform myTarget;
public Transform[] muzzlePositions;
public Transform turretBall;
private float nextFireTime;
private float nextMoveTime;
private Quaternion desiredRotation;
private float aimError;
void Start (){
}
void Update (){
if(myTarget)
{
if(Time.time >= nextMoveTime)
{
CalculateAimPosition(myTarget.position);
turretBall.rotation = Quaternion.Lerp(turretBall.rotation, desiredRotation, Time.deltaTime*turnSpeed);
}
if(Time.time >= nextFireTime)
{
FireProjectile();
}
}
}
void OnTriggerEnter ( Collider other ){
if(other.gameObject.tag == "Ground Enemy")
{
nextFireTime = Time.time+(reloadTime*.5f);
myTarget = other.gameObject.transform;
}
}
void OnTriggerExit ( Collider other ){
if(other.gameObject.transform == myTarget)
{
myTarget = null;
}
}
void CalculateAimPosition ( Vector3 targetPos ){
Vector3 aimPoint= new Vector3(targetPos.x+aimError, targetPos.y+aimError+1700//need to add 1700 or it is gonna aim stright down the ground
, targetPos.z+aimError);
desiredRotation = Quaternion.LookRotation(aimPoint);
}
void CalculateAimError (){
aimError = Random.Range(-errorAmount, errorAmount);
}
void FireProjectile (){
nextFireTime = Time.time+reloadTime;
nextMoveTime = Time.time+firePauseTime;
CalculateAimError();
foreach(Transform theMuzzlePos in muzzlePositions)
{
Instantiate(myProjectile, theMuzzlePos.position, theMuzzlePos.rotation);
}
}
}