Question by
yvyv1000 · Oct 19, 2015 at 08:49 AM ·
scriptingbasicsshoot
why does my enemy instant spawn bullets
so i have a script and it looks like this:
var target : Transform; // The player
var safeDistance = 10; //How close the player can be
var Bullet : Rigidbody;
var Barrel : Transform;
var ShootDelay = 2;
function Update(){
//check how close the player is
var distanceToPlayer = Vector3.Distance(this.transform.position, target.transform.position);
// checks if player is still in safeDistance or otherwise it will call shoot function
if(distanceToPlayer <= safeDistance){
Shoot();
}
}
//this is the shoot function
function Shoot(){
var rocketInstance : Rigidbody;
rocketInstance = Instantiate(Bullet, Barrel.position, Barrel.rotation);
rocketInstance.AddForce(Barrel.forward * 5000);
yield WaitForSeconds(ShootDelay);
// needs to wait for ShootDelay seconds and then do it again
}
but why is my enemy not waiting 2sec before firing again.
thanks for helping me
Comment
Best Answer
Answer by yvyv1000 · Oct 20, 2015 at 03:49 PM
already fixed it by deleting the
yield WaitForSeconds(ShootDelay);
in line 24
and replacing just after
Shoot();
in line 14