Turret AI Script

Hello,

I’ve created a turret script out of several tutorials, an enemy AI turret that will fire at the player helicopter, but it doesn’t seem to detect the target that it should fire at. The objectives of the script are as follows :

*A 3 dimensional rotation of the turret
*Able to fire a projectile that can be dodged from the target
*A detection range that the turret “looks” at the target and fires when the target gets close
*The projectile spawns from the projectile spawn point
*The projectile flies depending on how fast the variable input is

This is the script:

var lookAtDistance = 15.0;

var distance;

//Target to be aimed at

var findTarget : Transform;

//Projectile/Ammo from Turret

var ammoPrefab : Transform;

//Tells Unity When to fire

private var fireCycle : float;

//Fire Delay

var fireDelay : float;

function update() {

distance = Vector3.Distance(findTarget.position, transform.position);

if(distance < lookAtDistance){

//Follows target

transform.LookAt(findTarget);

}

//Check if Turret can fire

if(Time.Time > fireCycle){

//Fire

shoot();

//Update Firing Time

fireCycle = Time.Time + fireDelay;

}

}

//Turret Fires

function shoot(){

var ammo = Instantiate(ammoPrefab.transform.Find(“fireOutput”),transform.position,

Quaternion.identity);

ammo.rigidbody.AddForce(transform.forward * 500);

}

Any advice would be appreciated.

I’ve solved the problem to my script, funny that all it takes is a few tweaks.

distance = Vector3.Distance(findTarget.**transform.**position, transform.position);

var ammo = Instantiate(ammoPrefab,transform.Find(“fireOutput”).transform.position , transform.rotation);

It finally works now, heh, thanks anyway for your effort to look into my Question, Timsk.