- Home /
What to add to this script to control ROF
Here is my shooting script.....
var Bullet : Transform;
function Update () {
if (Input.GetButton ("Fire1")){
var bullet = Instantiate(Bullet,GameObject.Find("BulletSpawn").transform.position,Quaternion.identity);
bullet.rigidbody.AddForce(transform.forward *2000);
Destroy(bullet.gameObject, 2);
}
}
Im new to javascript and I'm trying to learn it. Since I have Input.GetButton its automatic fire, but its way to fast. Can somone show me how to creat a variable in the inspector; in the script, so I can change the Rate of Fire speed for different guns?? Thank you for the time.
Answer by Bunny83 · Apr 30, 2011 at 01:20 AM
Well, you need some sort of timeout to prevent shooting while the gun is "cooling down".
that should do the job:
var Bullet : Transform; var ROF = 10.0; // Rate of fire in bullets/sec. private var timeout = 0.0;
function Update () { if (Input.GetButton ("Fire1") && Time.time >= timeout){ var bullet = Instantiate(Bullet,GameObject.Find("BulletSpawn").transform.position,Quaternion.identity); bullet.rigidbody.AddForce(transform.forward *2000); Destroy(bullet.gameObject, 2); timeout = Time.time + 1.0/ROF; } }
Your answer
Follow this Question
Related Questions
machinegun don t work 2 Answers
How shot a bullet faster than the frame does? 2 Answers
how can i draw many (1000+) sprites 1 Answer
Standard Asset First Person Controller FPS Problems 0 Answers
First Person Bow 1 Answer