- Home /
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.
It seems "var LookAtTarget : GameObject[] = GameObject.FindGameObjectsWithTag("Enemy");" Returns array/multiple objects, so which one of those you want to look at?
Otherwise it should work as: "transform.LookAt(LookAtTarget.transform);"
Answer by DayyanSisson · Oct 29, 2011 at 05:23 AM
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;
}
}
Answer by roamcel · Aug 17, 2011 at 05:48 AM
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);
Do I completely replace my update function with this?
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];
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.transform);
}
}
Wait, I'm still confused about the set enemy target. I added that above the function update. I'm not sure what to put in it. Please explain. (thanks for the function update clear up)
Ok, I tried what you did and this is how my script came out:
var distanceTillShoot : float;
var LookAtTarget : GameObject;
var damp : float = 5.0;
var bulletPrefab : GameObject;
var savedTime = 0;
var enemieslist : GameObject[] = GameObject.FindGameObjectsWithTag("Enemy");
var currentenemyindex = 0;
function setcurrentenemy() {
//scan enemieslist and set currentenemyindex to the value of interest
}
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];
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.transform);
}
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 the errors I'm getting with the script have to do with the function Shoot(seconds), not the rest of the script, and the errors make no sense. Here's what I'm getting:
Assets/Void Runners/Scripts/Turret AI.js(42,10): BCE0044: expecting (, found 'Shoot'.
That makes no sense because Shoot HAS to come first for it to work. Here's another one:
Assets/Void Runners/Scripts/Turret AI.js(42,24): UCE0001: ';' expected. Insert a semicolon at the end.
That still makes no sense because it's a function and doesn't need a semi-colon.
And then line 43 supposedly has two errors: Unexpected token:var, & another semi-colon missing.
None of this makes any sense to me. Could someone help? (The script I'm using is below this comment)
Your answer
Follow this Question
Related Questions
HELP! i want to write to look at the target and give a force to follow 3 Answers
FindWithTag in certain range 2 Answers
Why is FindGameObjectsWithTag ignoring my Physics.OverlapSphere? 1 Answer
c# Get target gameobject from a list 1 Answer
TwoBoneIKConstraint Target wont update from script 0 Answers