- Home /
Question by
fireomega · Feb 15, 2012 at 06:05 PM ·
instantiateshootingbulletgun
Control amount of bullets
Im using the script
var projectile : Rigidbody;
var speed = 50;
function Update () {
clone = Instantiate(projectile, transform.position, transform.rotation);
projectile.tag = "Bullet";
clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
Destroy(clone.gameObject, 3);
}
How can i make it so i can control the amount of bullets instantiated each second
Comment
Answer by LegionIsTaken · Feb 15, 2012 at 06:50 PM
var projectile : Rigidbody;
var speed = 50;
var fireRate = 0.11;
private var lastShot = -10.0;//Dont touch!
function Update () {
if(Time.time > fireRate+lastShot){
clone = Instantiate(projectile, transform.position, transform.rotation);
projectile.tag = "Bullet";
clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
lastShot = Time.time;
}
Destroy(clone.gameObject, 3);
}
You use a simple if statement to check if a certain time has passed so we can fire again.