I’m glad you’ve liked it!
What it’s basically doing is spawning projectiles at a certain degree, depending on variable shotCount.
The code is kinda heavy, but I’m sure mine isn’t the most efficient
IEnumerator FireFromPoint()
{
while (true)
{
yield return new WaitForSeconds(firingPeriod);
PlayShotSFX();
if ((shotCount % 2) == 0)
{
FireIfEven();
}
else
{
FireIfOdd();
}
}
}
private void FireIfEven()
{
for (int i = 0; i < shotCount / 2; i++)
{
CreateProjectileInstance(angleInRad * (i + 0.5f)); //creating projectiles on the right
CreateProjectileInstance(-angleInRad * (i + 0.5f)); //creating projectiles on the left
}
}
private void FireIfOdd()
{
CreateProjectileInstance();
for (int i = 1; i <= shotCount / 2; i++)
{
CreateProjectileInstance(angleInRad * i); //creating projectiles on the right
CreateProjectileInstance(-angleInRad * i); //creating projectiles on the left
}
}
private void CreateProjectileInstance()
{
if (projectilePrefab)
{
GameObject projectile = Instantiate(
projectilePrefab, //prefab
transform.position, //position
Quaternion.identity); //rotation
projectile.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileVelocity);
}
else Debug.LogError("Missing object projectilePrefab, " + gameObject.name);
}
private void CreateProjectileInstance(float angleInRad)
{
if (projectilePrefab)
{
//velocity vector
Vector2 velocity = new Vector2(
projectileVelocity * Mathf.Sin(angleInRad),
projectileVelocity * Mathf.Cos(angleInRad));
//rotation in velocity vector direction
Quaternion rotation = Quaternion.AngleAxis(-angleInRad * Mathf.Rad2Deg, new Vector3(0, 0, 1));
GameObject projectile = Instantiate(
projectilePrefab, //prefab
transform.position, //position
rotation); //rotation
projectile.GetComponent<Rigidbody2D>().velocity = new Vector2(velocity.x, velocity.y);
}
else Debug.LogError("Missing object projectilePrefab, " + gameObject.name);
}