Shortening a lines of code.

I’m trying to shorten this code in someway. Any tips?

else if (bulletType == BulletType.DoubleShot && currentDoubleShotAmmo > 0) {
            nextFire = Time.time + DoubleShotRate;
            var doubleShot1 = Instantiate (Projectile, ProjectilePos.position, Quaternion.identity);
            var doubleShot2 = Instantiate (Projectile, ProjectilePos.position, Quaternion.identity);
            Vector2 temp = ProjectilePos.transform.position;
            doubleShot2.transform.position = temp;
            doubleShot2.transform.Translate (FirstBulletTranslateX, FirstBulletTranslateY, 0, Space.World);
            doubleShot1.transform.position = temp;
            doubleShot1.transform.Translate (SecondBulletTranslateX, SecondBulletTranslateY, 0, Space.World);
            currentDoubleShotAmmo--;
        }

Basically what the code does is this
https://giphy.com/gifs/xT9IggCIpxiJiABLtC/html5

I’m just wondering if theres a shorter way to do it without making a new class, but just instead of all this lines of code, lessen it to a few.

You might consider object pooling to manage your bullets instead of instantiating and destroying large numbers of objects

Ignoring the actual implementation, why would you want to shorten it? If it works, is bug free and does not exhibit any performance issues then I say you’re wasting energy on it.

I’d certainly heed Nkoeppel’s advice too.

maybe this would work,

else if (bulletType == BulletType.DoubleShot && currentDoubleShotAmmo > 0)
{
  nextFire = Time.time + DoubleShotRate;

  Instantiate(Projectile, ProjectilePos.position + SecondBulletTranslateXYZ, Quaternion.identity);
  Instantiate(Projectile, ProjectilePos.position + FirstBulletTranslateXYZ, Quaternion.identity);

  currentDoubleShotAmmo--;
}
1 Like