- Home /
IEnumerator not looping correctly?
I have a pretty basic burst fire function for a gun- as in shooting three times in a row. Here is the script:
void Update ()
{
timer += Time.deltaTime;
if (Input.GetButton("Fire1") && timer >= fireRate && Time.timeScale != 0)
{
if (burstFire)
{
StartCoroutine(BurstShot());
}
}
}
IEnumerator BurstShot()
{
for(int i = 0; i < burstSize; i++)
{
timer = 0f;
Quaternion bulletRot = Quaternion.Euler(90, 0, 0);
GameObject newBullet = Instantiate(bulletObj, transform.position, transform.rotation * bulletRot) as GameObject;
newBullet.GetComponent<Rigidbody>().velocity = bulletSpeed * transform.forward;
currentAmmo--;
yield return new WaitForSeconds(burstRate);
}
}
The problem is, while the first projectile will shoot forward correctly, the next two projectiles will launch upwards in a near perfect arc. And for the life of me I can't figure out why. Why is everything in the coroutine repeating correctly except for the apparent rotation of the last two shots?
Do The last two shots go in the same direction? I'm not sure what you mean by "upwards in a near perfect arc."
The last two shots are projected in the correct direction, just at a different angle it seems. The last two shots seem to be instantiated at a difference of about 50 degrees on the X axis from the first shot, which causes them to project up into the air ins$$anonymous$$d of directly forward
Answer by NerdRageStudios · Jun 27, 2017 at 02:44 AM
Perhaps your bullets are colliding with each other when you instantiate them, thus screwing up their rotation?
I had thought this at first but the projectiles are set to ignore collision with themselves in the Project Settings Physics matrix. Also this same code works with my shotgun method, where it instantiates a specific number of projectiles to fire at once
Your answer
Follow this Question
Related Questions
Coroutoutine doesnt work properly. 3 Answers
IEnumerator's did not read "bool" after yield return new WaitForSeconds. 1 Answer
Working projectile scripts do not work due to if condition 1 Answer
yield return new WaitForSeconds () not working 1 Answer
Make multiple Projectiles move at the same Time... 0 Answers