I'm working on TDa tower defense game.
I'm trying to make my gun object to rotate and shoot first closed clone object"enemy"the nearest instance of the "enemy" object.
My problem here is my gun script rotaterotates only to the main enemy object and not at clone objectits cloned objects or even the closedclosest one !!
I need my gun to rotate and facingface the closedclosest enemy to himitself.
Here is my clone script:
public GameObject enemy;
public float spawnTime = 3.0f;
public Transform[] spawnPoints;
void Start ()
{
InvokeRepeating ("Spawn", spawnTime, spawnTime);
}
void Spawn ()
{
int spawnPointIndex = Random.Range (0, spawnPoints.Length);
Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
}
Gun script:
private GameObject target;
private float enemy_distance;
void Start()
{
// -------------
} // end Start
void Update()
{
target = GameObject.FindGameObjectWithTag("enemy");
enemy_distance = Vector3.Distance(transform.position, target.transform.position);
if (enemy_distance <= 20) {
Quaternion rotation = Quaternion.LookRotation(target.transform.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.fixedDeltaTime * 2);
transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, transform.localPosition.z);
}
}// end update