- Home /
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);
}
}
Answer by aldonaletto · Jun 25, 2012 at 04:41 AM
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= 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.
@bfludzinski: Here's the copy that does it...
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:
if(target.position.z <= 109) nextFire = Time.time + fireRate; // set new nextFire time...
var copy = Instantiate(Prefab,GameObject.Find("$$anonymous$$ini Gun Spawn").transform.position,transform.rotation); // create projectile...
copy.rigidbody.velocity = transform.TransformDirection(Vector3.forward * speed); // and shoot it
}
}
}
Added:
if(target.position.z <= 109)
In front of the nextFire setting, so it only resets the time when the player is out of range again.
Note that this is actually @aldonaletto's code, so if it works for you, just check his as correct.
Answer by whydoidoit · Jun 24, 2012 at 04:56 PM
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("$$anonymous$$ini 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("$$anonymous$$ini Gun Spawn").transform.position,transform.rotation);
copy.rigidbody.velocity = transform.TransformDirection(Vector3.forward * speed);
}
}
Your answer
Follow this Question
Related Questions
Load Level delay on collision C# 1 Answer
WaitForSeconds Problem [C#] 4 Answers
yield new wait for seconds: what does "new" do? 1 Answer
How do I play simultaneous sounds one at a time? 2 Answers
C# wait X-Seconds in Code before continue in Code? 3 Answers