Projectile Firing Bug - Glitch Garden

Hey there,
I’m using an archer as my defender (instead of the cactus) so I wanted to implement a delay between when the projectile is instantiated and when it is fired from the arrow.
after some messing around, i finally got a coroutine that does fire arrows at the right time (previously my arrows were moving and then pausing when a new arrow was instantiated).
My issue is that the fired arrow will slow down once a new arrow is instantiated and then it will speed up again.
Does anyone have any ideas for a fix?
I’ve included video of what is happening, screenshots of my relevant code.

Archer script:

[SerializeField] GameObject bow;
[SerializeField] GameObject arrow;

public void LoadArrow()
{
    Instantiate(arrow, bow.transform.position, Quarternion.identity)

Projectile script:

[SerializeField] float arrowLoadTime = 1f;
[SerializeField] float arrowSpeed = 1f;

private void Start()
{
    archer = FindObjectOfType<Archer>();
}

private void Shoot()
{
    StartCoroutine(ArrowFly());
}

IEnumerator ArrowFly()
{
    while (true)
     {
       yield return new WaitForSeconds(arrowLoadTime);
       transform.Translate(arrowSpeed * 
       Time.deltaTime. * Vector2.right);
     }

Hi Jesse,

Please refrain from posting screenshots of your code because it’s impossible to copy and paste lines from them. Instead, paste it here and format it properly.

The problem might be the loop in the coroutine and the Shoot method in Update which created a new coroutine each frame. What you want to do is to “load” the arrow and then move it constantly. This means that the delay must not be part of the loop. Also do not call a new coroutine each frame.

Hopefully, this helped. :slight_smile:


See also:

1 Like

Im sorry about that…i’ll edit the post so it has code in the right format.

Also thanks for the tips, im gonna test out some things

Thanks for your help.
I tweaked my code and removed the “while (true)” statement from the coroutine and it worked just fine!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms