- Home /
Raycast Shooting Firerate?
Hi, so I'm making a first person shooter and I have a shooting script using raycast, here it is
#pragma strict
function Start () {
}
function Update () {
if(Input.GetButton("Fire1")){
var fwd = transform.TransformDirection (Vector3.forward);
if (Physics.Raycast (transform.position, fwd, 1000)) {
print ("There is something in front of the object!");
}
}
}
It works just fine but it fires every frame and I want to put a fire rate on it so it will fire a certain amount of shots a second. Any ideas? Thanks in advanced.
Comment
If the rate is slower than the frame rate you can put it in its own method in an infinite loop with a yield for the appropriate amount of time.
Best Answer
Answer by flaviusxvii · Feb 10, 2013 at 04:24 PM
You can make yourself a little timer. (Pseudocode follows..)
var timeSinceLastShot : float = 0f;
var timeBetweenShots : float = 1 / (shots per second);
void Update() {
timeSinceLastShot += Time.deltaTime;
if(timeSinceLastShot >= timeBetweenShots) {
shoot();
timeSinceLastShot = 0f;
}
}