Help adding multiple directions to projectiles

I’ve been trying to work out how setup a system allowing for projectiles to be shot in multiple directions from the same prefabbed object for my extended version of Laser Defender. Below is what I’ve come up with. Can anyone provide feedback on this?

I’d really appreciate a sanity before I break my game… again :wink:

Current System

Weapon prefabs consist of parent GameObject with WeaponConfig.cs containing speed and firing period. Parent GameObject has childed GameObjects containing the projectiles. This is the WeaponConfig.cs script.

public class WeaponConfig : MonoBehaviour {

    [SerializeField] float projectileSpeed = 10f;
    [SerializeField] float projectileFiringPeriod = 0.5f;
    

    public float GetProjectileSpeed()
    {
        return projectileSpeed;
    }

    public float GetProjectileFiringPeriod()
    {
        return projectileFiringPeriod;
    }
}

Weapon prefabs are instantiated via the Player’s FireContinuously method as per below;

private IEnumerator FireContinuously()
{
	while(true)
	{
		GameObject projectile = Instantiate(weapon, transform.position, Quaternion.identity) as GameObject;
		projectile.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
		soundManager.TriggerPlayerShotSFX();
		yield return new WaitForSeconds(projectileFiringPeriod);
	}
}

Here’s what the prefabs currently look like in the Hierarchy and Inspector.
image

image

Potential System Adding Projectile Directions

Add a list to WeaponConfig containg floats representing the X value to be passed to projectile.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed)
E.g.

public class WeaponConfig : MonoBehaviour {

    [SerializeField] float projectileSpeed = 10f;
    [SerializeField] float projectileFiringPeriod = 0.5f;
    [SerializeField] List<float> projectDirectionList;  // Populated in inspector

    public float GetProjectileSpeed()
    {
        return projectileSpeed;
    }

    public float GetProjectileFiringPeriod()
    {
        return projectileFiringPeriod;
    }
	
	public float GetProjectileDirection(float directionListElement)
	{
		return projectDirectionList[directionListElement];
	}
}
private IEnumerator FireContinuously()
{
	while(true)
	{
		GameObject projectile = Instantiate(weapon, transform.position, Quaternion.identity) as GameObject;
		projectile.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
		
		// Get children of projectile GameObject
		List<Rigidbody2D> projectileRigidBodies = projectile.transform.GetComponentsInChildren<Rigidbody2D>();
		
		// Set rBodyCounter for iterating through projectDirectionList
		int rBodyCounter = 0;
		
		// For Each child Of projectile
		foreach (rBody in projectileRigidBodies)
		{
			// Apply direction via Velocity
			rBody.velocity = new Vector2(projectDirectionList[rBodyCounter], projectileSpeed);
			
			// Iterate rBodyCounter
			bBodyCounter++;
		}
		
		soundManager.TriggerPlayerShotSFX();
		yield return new WaitForSeconds(projectileFiringPeriod);
	}
}
1 Like

Hi Jeff,

Sounds like a very workable solution.

Just a thought, if I’ve understood correctly, what you’d have is one prefab for a projectile, and then an array with a set of directions, so you could create an array of 1, or 4, or 8 for example and have that specific type of prefab fly out in the directions specified.

I was thinking that you may encounter a scenario where you want to mix the projectile types that you fire. For example, maybe horizontal and vertical appear as little blue bullets and the diagonals in between, perhaps as an upgrade to that initial firing pattern, appear as red missiles whizzing out.

Your current system wouldn’t allow for that, as the variant is only the array size, so its only the directions you can change. You could, of course, add a second WeaponConfig.cs script component I suppose, which used a different prefab, and had a separate list of directions, but that may get challenging to tell apart in the Inspector.

You could break down the current approach a little further to give you more flexibility. As an example, you’d have a script which holds an array of weapon configs, and then each weapon config could be a unique prefab, with its specific direction and velocity, effectively what you have now, but one further level abstracted. That would give you the ability to create a varied mixture of velocities, prefabs and directions.

Taking this a step further, you could prefab that ground of configs to create your firing patterns, which you could then instantiate on the collection of a power-up, for example, or revert back to a specific prefab’d firing pattern after a power-up has ran out.

The above may be a little more complicated than you wanted, but I thought I’d offer it up as a suggestion anyway, on the off chance it’s of use.

1 Like

Just a thought, if I’ve understood correctly, what you’d have is one prefab for a projectile, and then an array with a set of directions, so you could create an array of 1, or 4, or 8 for example and have that specific type of prefab fly out in the directions specified.

Close. It’s an empty gameObject with a RigidBody and WeaponConfig attached like this;

image

image

Then the individual projectiles nested underneath look like this

image

I was thinking that you may encounter a scenario where you want to mix the projectile types that you fire. For example, maybe horizontal and vertical appear as little blue bullets and the diagonals in between, perhaps as an upgrade to that initial firing pattern, appear as red missiles whizzing out.

I think I could achieve this by simply adding different projectiles underneath the parent with the weapon config indicating direction for each. However I do like the idea of having each projectile as it’s own prefab with an attached ProjectileConfig which would store it’s direction, speed and damage.

You could break down the current approach a little further to give you more flexibility. As an example, you’d have a script which holds an array of weapon configs, and then each weapon config could be a unique prefab, with its specific direction and velocity, effectively what you have now, but one further level abstracted. That would give you the ability to create a varied mixture of velocities, prefabs and directions.

Taking this a step further, you could prefab that ground of configs to create your firing patterns, which you could then instantiate on the collection of a power-up, for example, or revert back to a specific prefab’d firing pattern after a power-up has ran out.

Here I lost the thread. Could be I’ve been looking at this stuff a little too long but I couldn’t follow sorry.

Reviewing a bit further, something I can’t yet think of is how to have mixed firing rates on the same weapon. For example in this video, bullets are firing at one rate while missiles another.

Here’s what I’ve got working ATM. Still would be good to get alternate firing periods/speed for different projectiles but this is good for now.

1 Like

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

Privacy & Terms