- Home /
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
[1]: /storage/temp/47939-1.png
[2]: /storage/temp/47940-2.png
Answer by DiegoSLTS · Jun 10, 2015 at 12:56 AM
"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:
There should be a line of code that starts the first fire, right? Go to that line.
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.
Thanks for your answer, I appreciate it
I have tried this implementation, but it yields the same result. The Shooting script for my space ship looks like this at the moment:
void Start () {
objectPooler = GetComponent<ObjectPooler>();
//Repeats the invoke, but yields the same result
InvokeRepeating("Fire", fireDelay, firerate);
}
private void Fire(){
//Gets a bullet from an object pooler
GameObject obj = objectPooler.GetPooledObject();
if(obj == null) return;
obj.transform.position = transform.position;
obj.transform.rotation = transform.rotation;
obj.SetActive(true);
}
I figured that the Instantiation would take time. This implementation however makes it so that all bullets gets instantiated under the Start method once. Every bullet is then ready to be fired once the "Fire" method gets called for the first time. When using this implementation with an object pooler, the bullets are never destroyed or instantiated, thus it can not delay the repeating invoke call.
Any other ideas? Thanks again.
Your answer
Follow this Question
Related Questions
Bullets won't fire? 0 Answers
Cannot destroy instantiated objects 2 Answers
Trying to make an enemy fire at the player 1 Answer
Bullet always facing the sky. 3 Answers
bullet issue 1 Answer