Im wanting to make a script where whenever you get within Range #1 of a turret, the turret will look at you then start moving towards you. Then, when you get within Range #2, it will start shooting at you. I keep getting the expecting : found ; error and when I do as it says, it loads 3 new errors. Could someone please fix the script to where my intentions may be achieved? Here is the script:
var distanceTillShoot = 30;
var distanceTillLook = 15;
var target : Transform;
var damp = 6.0;
var laserPrefab : Transform;
var savedTime = 0;
var laserSpeed = 10000;
var targetTag : String;
var lookSwitch : Boolean = false;
var movementSpeed = 5;
function Start ()
{
var tar = GameObject.FindWithTag(targetTag);
if(tar)
lookAtTarget = tar.transform;
}
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 % 50);
*/
if(lookAtTarget)
{
Look(seconds);
if(lookSwitch == true)
{ Shoot(seconds);
}
}
}
function Look(seconds)
{
var distance = Vector3.Distance(LookAtTarget.transform.position, transform.position);
if (distance <= distanceTillLook)
{
if(seconds!=savedTime)
lookSwitch = true;
}
{
transform.lookAt(target);
transform.Translate(Vector3.forward*movementSpeed*Time.deltaTime);
}
function Shoot(seconds)
{
var distance = Vector3.Distance(LookAtTarget.transform.position, transform.position);
if (distance <= distanceTillShoot)
{
if(seconds!=savedTime)
var laser = Instantiate(laserPrefab,transform.Find("spawnPointCanon").transform.position , transform.rotation);
}
{
gameObject.tag : "enemyProjectile";
laser.rigidbody.AddForce(transform.forward * laserSpeed);
}
{
savedTime=seconds;
}
if (distance > distanceTillShoot)
{
lookSwitch = false;
if(seconds!=savedTime)
}
}
This code is fixed, but it doesn’t look complete; there are two variables that haven’t been declared (LookAtTarget and seconds), there is also an unfinished if statement toward the end…
But the compile errors are fixed.
There’s a couple of things I think will help you, if you remember to do them:
whenever you start a new function or if statement, go ahead and type the closing brace, and type inside of the braces.
This way, you’ll never forget to add it.
always line up the closing braces with the line that they are closing, and indent anything inside. In other words, make sure the contents of the if statement are indented, and the closing brace is lined up even with the if line.
var distanceTillShoot = 30;
var distanceTillLook = 15;
var target : Transform;
var damp = 6.0;
var laserPrefab : Transform;
var savedTime = 0;
var laserSpeed = 10000;
var targetTag : String;
var lookSwitch : boolean = false;
var movementSpeed = 5;
function Start () {
var tar = GameObject.FindWithTag(targetTag);
if(tar) {
lookAtTarget = tar.transform;
}
}