Uneven firerate

Hello. I noticed a weird thing that is happening when i try to shoot some bullets. It appears that the bullets are not getting instantiated at an even rate (see attached picture 1)

The first attached picture is a screenshot of a simple test scene.
The code used in this scene for the instantiation is:

	public GameObject cube;
	public float fireRate = 0.075f;
	
	void Start () {
		Fire();
	}

	void Fire(){
		Instantiate(cube, transform.position, Quaternion.identity);
		Invoke("Fire", fireRate);
	}

And for moving the bullets:

	void Update () {
		transform.Translate(0, 4 * Time.deltaTime, 0);
	}

The second attached picture shows a SHMUP game that I’m working on. It’s the same concept but the fire mechanics utilizes the ObjectPooling script (taken from the Live Training Session) instead.

Q: Why are the bullets spawning with an uneven rate when I increase the fire rate? Is Unity unable to handle this? Have I coded something funky?

This looks really ugly and I don’t know how to fix it, any help would greatly be appreciated!

Thanks in advance

“Invoke(“Fire”, fireRate);” will execute the “Fire” method after fireRate seconds, counting from the moment that line was executed. Before calling that line you Instantiate an object, that operation takes time. If you make fireRate too small, the Instantiate line might be delaying the Invoke call a significant amount (the smaller the fireRate, the bigger the impact of the delay).

I’m not sure why you did that kind of “recursive” call to create fires, it’s like a fire creates the next, instead of a weapon creating them.

Why not do something like this:

  1. There should be a line of code that starts the first fire, right? Go to that line.

  2. Instead of Instantiating one fire and letting the rest instantiate themself, write a function that instantiates the fire, and call that function repeatedly. Something like this:

    //instead of Instantiating the first fire…
    InvokeRepeating(“Fire”,fireRate,fireRate);

    //In the same script add this function
    void Fire() {
    Instantiate(cube, transform.position, Quaternion.identity);
    }

This way each invokation will be at the same fireRate, and the Instantiate will ocurr during that time between fires.