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.