I start the game, a unit gets spawned and the target gets selected so that I can kill it. If I do not kill it and a second unit spawns then I want to run out of range of the first one witch is 20Units and then should pick the next unit within 20 Units.
What it currently does is stays on the same target until its dead even if I run 20units away from it.
Also want the target to be acquired only when I am facing the enemy unit.
var damp = 6.0;
var BulletPrefab:Transform;
var bulletSpeed = 1000;
function Update ()
{
//Assign Enemy
var LookAtTarget : GameObject ;
LookAtTarget = GameObject.FindWithTag("EnemyAI");
//Calculate Distance from enemy
var dist = Vector3.Distance(transform.position, LookAtTarget.transform.position);
if (dist > 20) {
LookAtTarget = null;
}
//Only detect enemy withing range of 20meteres
//If target aquired then do
if(LookAtTarget)
{
//Rotate Gun
var rotate = Quaternion.LookRotation(LookAtTarget.transform.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
//Aquire Target
GameObject.FindWithTag("Crosshair").guiTexture.enabled = true;
GameObject.FindWithTag("Crosshair").GetComponent("TextFollowScript").target = LookAtTarget.transform;
//Shoot if Fire button pressed
if (Input.GetButtonDown("Fire1")){
var Bullet = Instantiate(BulletPrefab, transform.Find("SpawnPointT").transform.position, Quaternion.identity);
Bullet.rigidbody.AddForce(transform.forward * bulletSpeed);
}
}
//If no target then do
if(!LookAtTarget) {
GameObject.FindWithTag("Crosshair").guiTexture.enabled = false;
}
}