Controlling Fire Rate - Instantiating bullet objects at a specified rate

Hi, I’ve been trying a few different methods for controlling fire rates but so far have had little success. So my question is what techniques have people been using for their games to control fire rates?

Below are the methods I’ve tried and the problems I had with them:

The first method i tried was to use a counter which increased every frame when isFiring is true. Then when the counter hit the specified fire rate the bullet object was instantiated and the count reset. The problem this seemed to cause what that it was not frame rate independent causing it to vary wildly on different machines.

The second method was to mimic the process of chambering a round by invoking a method at the specified time which set the roundChambered variable to true. The bullet was only instantiated when roundChambered was true. In theory, I think this method should have worked, however, a few major problems arose. The first was the players fire rate was very sporadic. It would work fine for a few seconds then pause for too long then fire a burst. The enemy weapons did the same however, they also appeared to increase in speed over time. I think this may be due to too much computation going on but not sure.

The final method i tried was to use InvokeRepeating in the start method of each weapon. This was setup to call the fire method at the designated time continuously through the life of the object. The fire method then had a conditional statement requiring the isFiring method to be true to actually instantiate the bullet. The problem I had with this was when the player picked up another weapon I was not able to alter the rate at which the fire method was invoking and therefore, was not able to change the weapons fire rate. Another problem was the lack of responsiveness from the player weapon as pressing fire only sets the isFiring variable to true. The player then has to wait for the fire method to be invoked again before the bullet is instantiated.

1 Like

Wouldn’t what we did in Laser Defender work for that?

It uses InvokeRepeating, but it doesnt do it in Start(), it does it in Update()

if (Input.GetKeyDown (KeyCode.Space)) 
{
    InvokeRepeating ("Fire", 0.0001f, fireRate);
}
		
if (Input.GetKeyUp (KeyCode.Space)) 
{
    CancelInvoke ("Fire");
}
2 Likes

Wow I’d completely forgotten we did that in Laser Defender. Yeah that seems like it would work. I’ll give it a go. Thanks heaps for that!

Worked perfect. Thanks!

1 Like

Glad to help!

1 Like