Simple shooting script causing heavy lag

Hi all,

I have two scripts, a WeaponController (which instantiates a bullet prefab and sets the direction) and a Projectile script which fires it (by translating it forwards).

For some reason when I fire a number of bullets it seems to lag the game very heavily and I’m not quite sure what this is down to. The bullet prefab has a Projectile script attached.

If anyone can help that would be great :slight_smile:


public class Weapon_Controller : MonoBehaviour
    // The weapons that are available
    public GameObject[] weaponPrefabs;

    // The position to fire the weapon from - Right bullet spawner
    public Transform BulletSpawnerR_obj;

    private Projectile tempProjectile;

   // Shoot!
    public void Shoot()
        // Instantiate a new projectile at the bullet spawner position
        GameObject newProjectile = Instantiate(weaponPrefabs[currentWeapon],
                                   BulletSpawnerR_obj.rotation) as GameObject;

        // Access the Projectile script on the projectile just instantiated.
        // Calling the script will fire the projectile straight away (called 
        // inside its Start())
        tempProjectile = newProjectile.GetComponent<Projectile>();

        // Make the targetPoint transform point in the bullet spawner's 
        //  forward direction
        tempProjectile.targetPoint = BulletSpawnerR_obj.forward;

        //Set the weaponControllerReference to point to the current projectile
        //  just instantiated.
        tempProjectile.weapon_ControllerReference = this;

Projectile script

public class Projectile : MonoBehaviour 
    // This is the target at which the projectile will be fired
    public Vector3 targetPoint =;

    // Use this for initialization
	void Start () 
        // Make the projectile face the target position

        // Destroy the projectile after a delay
        Destroy(gameObject, timeToLiveInSeconds);

        // Call the DoMovement() coroutine to fire the projectile forward

    IEnumerator DoMovement()
        // While the object that this script is attached to exists..
        while (true)
           // Move projectile forward
           transform.Translate(targetPoint * Time.deltaTime * 100);


        // Waits until next fixed frame rate update function.
        yield return new WaitForFixedUpdate();



The while(true) loop in your DoMovement coroutine looks to be causing the lag.

I assume you want the projectile to constantly travel in a given direction?
You would be best off using FixedUpdate instead of a coroutine.

void FixedUpdate()
    transform.Translate(0, 0, 1, Space.Self);

This will move the gameObject that the script is attached to forward by 1 unit each FixedUpate. Adjust the 1 if you’d like it to move faster or slower.

In your coroutine you have an infinite loop that never exits. So each projectile you spawn creates a new blocked coroutine that eats CPU. My guess is you wanted to put the yield into the wile loop?

However, don’t use a coroutine for this simple translation. Since all you do is to wait for the next FixedUpdate anyway, just put the transform.Translate directly into FixedUpdate();