- Home /
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 :
3 dimensional rotation of the turret *Able to fire a projectile that can be dodged from the target
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'd love to help you, I like doing AI. However, there are a few problems with your question, firstly:
it doesn't seem to detect the target that I'm firing
I don't understand that statement, does it not detect the target, or does it not detect the firing? It's a confusing sentence.
Also, I'm not entirely sure what your problem is, you have given us a blob of code and told us what you want it to do. What problems are you having? What have you tried and why didn't it work?
Also:
This is the script that I'm currently following:
Is this a script from a tutorial your following, or a script you have written yourself and is not working as intended? Again, another confusing sentence.
Alright, sorry about the typo errors, I was in a hurry.
The turret(enemy) doesnt detect the helicopter(player) when I used this script.
I tried to follow this tutorial from the video here.
http://www.youtube.com/watch?v=tL-sn6r9YBw
It didn't work for me, then I tried to look at other answers from UnityAnswers and tried to fix it, but I'm stumped on why the script still doesnt work.
Hope that cleared up the misinformation.
Answer by LordJulian · Nov 15, 2011 at 08:55 AM
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.
Your answer
Follow this Question
Related Questions
enemy AI script 3 Answers
Tank AI script that calculates distance of targets and firing angle 1 Answer
[Solved]How to destroy a turret in Tower Defense game 2 Answers
Trouble with turret script 1 Answer
Animay play in editor 0 Answers