Firing to Target In Range
I'm creating an arcade game and I created some turrets called "turbolaser". I created a script for these turrets to shoot continously with 1 sec. wait before firing again. No problems occured; here's the script:
#pragma strict
var shot : GameObject;
var shotSpawn : Transform;
var fireRate : float;
private var nextFire : float;
function Update () {
if (Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
}
}
But then, I tried to stop them from shooting when the player is too far, and configured the script but it didn't work:
#pragma strict
var shot : GameObject;
var shotSpawn : Transform;
var fireRate : float;
var distance : float;
var player : Transform;
var turbolaser : Transform;
private var nextFire : float;
function Update () {
distance = Vector3.Distance (player - turbolaser);
if (Time.time > nextFire)
{
if(distance <= 20.0f){
nextFire = Time.time + fireRate;
Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
}
}
}
Then I get this error: Operator "-" cannot be used with a left hand side of type UnityEngine.transform and a right hand side of type UnityEngine.transform.
I understand what the error is telling me, but what should I do ?
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612064926im_/https://answers.unity.com/themes/thub/images/avi.jpg)