Wait five seconds

I’m making a script that when my player crosses a line a turret points and shoots at me I wrote all of that but now I want it to look at me , wait five seconds then shoot at me I tried looking at unity script reference but the stuff they give you can’t be used in function Update()
here is my script:

var target : Transform;
var rotationspeed : float = 30;
var fireRate : float = 0.1;
var Prefab: Transform;
private var nextFire = 2.0;
var speed : float = 50;
function Update(){
transform.Rotate(Vector3.up * Time.deltaTime * rotationspeed);
if(target.position.z>109){
transform.LookAt(target);
//here is where I want it to wait 5 seconds
nextFire = Time.time + fireRate;
    var copy = Instantiate(Prefab,GameObject.Find("Mini Gun Spawn").transform.position,transform.rotation);
   copy.rigidbody.velocity = transform.TransformDirection(Vector3.forward * speed);
    
}
}

2 Answers

2

There’s a simple way to do that with your current script: keep setting nextFire to Time.time+5 while the target is behind the shot range - this way there will be a 5 seconds delay when the player enters the shot range, and after that the shots will obey the fireRate interval:

function Update(){
  transform.Rotate(Vector3.up * Time.deltaTime * rotationspeed);
  if(target.position.z<=109){ // while player outside shot range...
    nextFire = Time.time + 5; // set nextFire to 5 seconds from now
  } else { // but when inside range...
    transform.LookAt(target); // look to the player...
    if (Time.time >= nextFire) { // and shoot when nextFire time reached:
       nextFire = Time.time + fireRate; // set new nextFire time...
       var copy = Instantiate(Prefab,GameObject.Find("Mini Gun Spawn").transform.position,transform.rotation); // create projectile...
       copy.rigidbody.velocity = transform.TransformDirection(Vector3.forward * speed); // and shoot it
    }
  }
}

now it just shoots one bullet every five seconds. i want it to wait five secondsand then rapidly shoot.

You can’t use Coroutines inside update but you can just start one using StartCoroutine from update:

  function Update() {
      if(somethingHappened) {
          StartCoroutine("ShootAtMe");
      }
  }

  function ShootAtMe() {
       yield new WaitForSeconds(5);
       //Start shooting
  }

heres my script: var target : Transform; var rotationspeed : float = 30; var fireRate : float = 0.1; var Prefab: Transform; private var nextFire = 2.0; var speed : float = 50; function Update(){ transform.Rotate(Vector3.up * Time.deltaTime * rotationspeed); if(target.position.z>109){ transform.LookAt(target); //here is where I want it to wait 5 seconds nextFire = Time.time + fireRate; var copy = Instantiate(Prefab,GameObject.Find("Mini Gun Spawn").transform.position,transform.rotation); copy.rigidbody.velocity = transform.TransformDirection(Vector3.forward * speed); } }

Could you try and post that code formatted (indent it in the edtior by 4 spaces or one tab before pasting) - I can't really seem to format it right :) We need to put the transform.LookAt(target) etc etc after your comment in the //Start shooting part of my answer and all of the stuff before that in the update - replacing somethingHappened in the first part of my answer with the target.position.z > 109

or ... in monodevelop, highlight all your code, then hit tab then paste that =]

var target : Transform; var rotationspeed : float = 30; var fireRate : float = 0.1; var Prefab: Transform; private var nextFire = 2.0; var speed : float = 50; function Update(){ transform.Rotate(Vector3.up * Time.deltaTime * rotationspeed); if(target.position.z>109){ transform.LookAt(target); //here is where I want it to wait 5 seconds nextFire = Time.time + fireRate; var copy = Instantiate(Prefab,GameObject.Find("Mini Gun Spawn").transform.position,transform.rotation); copy.rigidbody.velocity = transform.TransformDirection(Vector3.forward * speed); } }