AI Turret Targeting System

I have a modified turret script and there is a major problem: it’s designed to only look at one target. I modified this script to find objects with a certain tag. But it doesn’t do it. Here’s the script:

var distanceTillShoot : float;
var LookAtTarget : GameObject[] = GameObject.FindGameObjectsWithTag("Enemy");
var damp : float = 5.0;
var bulletPrefab : GameObject;
var savedTime = 0;

function Update ()
{
     if(LookAtTarget)
     {
          var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position); 
          transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp); 
          var seconds : int = Time.time;
          var oddeven = (seconds % 9999);
          if(oddeven) 
              Shoot(seconds);
          transform.LookAt(LookAtTarget);
     }

}

function Shoot(seconds)
{
 var distance = Vector3.Distance(LookAtTarget.transform.position, transform.position);
   if (distance <= distanceTillShoot){
     if(seconds!=savedTime)
     {
          var bullet = Instantiate(bulletPrefab,transform.Find("TSP").transform.position , transform.rotation);
     }


     savedTime=seconds;
    }
}

And I’m getting this error message:

BCE0023: No appropriate version of 'UnityEngine.Transform.LookAt' for the argument list '(UnityEngine.GameObject[])' was found.

I think the problem is that it’s trying to transform the position of a Transform, not a GameObject, but I need it to transform the position of a GameObject with Tag. I don’t know how to do this. Thanks in advance.

Update

Okay, I’ve tried a different approached with my own script:

#pragma strict
@script ExecuteInEditMode()
@script AddComponentMenu ("Void Runners : Towers/AI/Friendly/Friendly Turrets")
class FriendlyTurrets extends MonoBehaviour {
}

var primaryRange : float = 100;
var specialRange : float = 500;
private var target : Transform;
var damp : float = 2.0;
var primary : GameObject;
var primaryMuzzleFlash : GameObject;
var primaryAnimation : Animation;
var primaryFiringSound : AudioClip;
var special : GameObject;
var specialMuzzleFlash : GameObject;
var specialAnimation : Animation;
var specialFiringSound : AudioClip;
var primaryFireRate : float = 0.1;
var specialFireRate : float = 30;
private var nextFire : float = 0.0;
private var savedTime : float = 0;


function Update ()
{
          var rotate = Quaternion.LookRotation(target.position - transform.position); 
          transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp); 
          transform.LookAt(target);
}


function OnCollisionEnter () {

        var colls = Physics.OverlapSphere(transform.position, primaryRange);
    for (var col: Collider in colls){
    	target = col.CompareTag("Enemy");
        if (col.CompareTag("Enemy")){
            var distance = Vector3.Distance(col.transform.position, transform.position);
            	NormalAttack();            
            }
            else 
            if (distance <= specialRange){
            	SpecialAttack();
			}
	}
	
}


function NormalAttack () {
	
	if(Time.time > nextFire) {
        nextFire = Time.time + primaryFireRate;	
	Instantiate (primary, transform.position, transform.rotation);       	
	Instantiate (primaryMuzzleFlash, transform.position, transform.rotation);
	Animation.Play(primaryAnimation);
	audio.PlayOneShot(primaryFiringSound); 	  	
	}
	
}

function SpecialAttack () {
	
	if(Time.time > nextFire) {
        nextFire = Time.time + specialFireRate;	
	Instantiate (special, transform.position, transform.rotation);       	
	Instantiate (specialMuzzleFlash, transform.position, transform.rotation);
	Animation.Play(specialAnimation);
	audio.PlayOneShot(specialFiringSound);  	  	
	}
	
}

I can’t test this approach because I get this error:

Friendly Turret.js(37,32) BCE0022: Cannot convert ‘boolean’ to ‘UnityEngine.Transform’

Well line 37 is:

	target = col.CompareTag("Enemy");

So it’s treating the “target” variable as a boolean at first, then gives an error when I use it as a Transform. Well, “target” is set as an empty Transform in the variable list. I’m not sure what I wrote wrong.

Since I can’t test this, I don’t even know if this will work or not, because I have never tried this approach and I haven’t seen anyone who has. So if I fix that error, would this script even work? If it won’t work, I need to know why to see if I can change it. Your help would be greatly appreciated.

Your problem is based what @mgear spotted, but there's possibly a few other flaws. FindGameObjectsWithTag returns an array, so you need to change your code appropriately.

For a start, add two variables

var enemieslist : GameObject[] = GameObject.FindGameObjectsWithTag("Enemy");
var currentenemyindex = 0;

and change LookAtTarget

var LookAtTarget : GameObject;

Then you will need another function

function setcurrentenemy() {
  //scan enemieslist and set currentenemyindex to the value of interest
}

then update your update function

...
function Update ()
{
     //in your original code the enemy list is only populated once
     //with this instruction here, it'll get updated always, but since find with tag
     //is a slow function, you'll need to move it to somewhere more appropriate
     enemieslist = GameObject.FindGameObjectsWithTag("Enemy");
     if(enemieslist.Length>0)  //possibly a meaningless check at this point
     {
       setcurrentenemy();
       LookAtTarget = enemieslist[currentenemyindex];
     ...
     transform.LookAt(LookAtTarget.transform);

Okay, I found an answer myself. I just added this to the top of the Update function:

target = GameObject.FindWithTag("Human Fighter").transform;

Here’s the whole script:

var range : float = 100;
var target : Transform;
var damp : float = 2.5;
var bulletPrefab : GameObject;
var fireRate : float = 10.0;
private var nextFire : float = 0.0;
private var savedTime : float = 0;

function Update ()
{
	 target = GameObject.FindWithTag("Scorpion").transform;
	 
     if(target)
     {
          var rotate = Quaternion.LookRotation(target.position - transform.position); 
          transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp); 
          var seconds : float = Time.time;
          var oddeven = (seconds);
          if(oddeven) 
              Shoot(seconds);
          transform.LookAt(target);
     }
}

function Shoot(seconds)
{
 var distance = Vector3.Distance(target.transform.position, transform.position);
   if (distance <= range){
     if(seconds!=savedTime && Time.time > nextFire)   {
nextFire = Time.time + fireRate;
          var bullet = Instantiate(bulletPrefab,transform.Find("TSP").transform.position , transform.rotation);
     }
     savedTime=seconds;
    }
}