what i have done:
when enemy comes in the range of the tower the tower rotates towards the enemy and start firing…and keep rotating until it crosses the range…
but when the second enemy comes it dont move…
my code is like this
using UnityEngine;
using System.Collections;
public class TowerControleRotation : MonoBehaviour {
public Transform lookAtTarget;
public int range;
public Transform captureEnemy;
public GameObject EnemyInHand;
public int enemynumber=0;
public int countt=0;
void Awake()
{
if (!lookAtTarget)
{
lookAtTarget = GameObject.FindWithTag("enemy").transform;
}
void Update()
{
if(lookAtTarget && CanAttackTarget())
{
var rotate = Quaternion.LookRotation(lookAtTarget.transform.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, 1);
transform.Translate (0,0,0);
}
}
bool CanAttackTarget()
{
//check if in range
if(Vector3.Distance(transform.position, lookAtTarget.position) > range)
{
return false;
}
return true;
}
}