Help with modular projectile system

Hello,

I’m having trouble with a modular projectile system. Currently I have it so a Spawner has a function that fires projectiles, taking in the Projectile type, the pattern to fire them in and the origin transform. Currently im trying to add a sort of modifier system, where i can add unique effects once the projectile decays, hits something etc etc.

I’ve managed to create an on death effect:

public class Modifier : ScriptableObject, IModifier
{
    public void OnDecay(Transform transform)
    {
        Debug.Log("ondecay");
        var spawner = Instantiate(GameAssets.I.spawner);
        spawner.transform.position = transform.position;
        spawner.GetComponent<Spawner>().ShootPattern(spawner.transform, GameAssets.I.grenade, GameAssets.I.flower, default, true);
    }
}

My problem now however, is I’m unsure how to be able to create another modifier, that can be swapped in and out whenever i want.

Currently, the default OnDecay effect spawns 8 more projectiles in a flower shape, but for example, if i wanted it to explode or something, im unsure how i would be able to create another modifier that i can change the effect of, whilst still being able to input it into the same fields as the previous one.

Im going to keep at it, if i find a solution ill post it here, any help is appreciated, Thanks.

Hello sorry! I’ve managed to figure out my own solution before i saw your comments @andrew-lukasik . I’ve changed the modifiers to be MonoBehaviours. So i’ve got my base Modifier class

public class Modifier : MonoBehaviour, IModifier
{
    public virtual void OnDecay(Transform transform)
    {
        Debug.Log("default");
    }
}

Which just contains the default on decay method now, and instead i’ve opted to create seperate scripts that derive from modifier. I’ve got a list of those scripts in string form within my GameAssets script. Now, whenever i call the ShootPattern method, it takes a string in place of a modifier. It then uses this string to parse through the list in GameAssets to find if its a valid script and add the component to the bullet itself, whilst also giving the bullet a reference to the component. bullet calls that component on death etc etc. Component gets deleted along with bullet afterwards.

Heres the updated sections of the Spawner script

public void ShootPattern(Transform origin, Projectile projectile, ProjectilePattern pattern, string mod = null, bool temp = false)
    {
        //randomize the pattern if it needs to be
        if (pattern.random)
        {
            for (int i = 0; i < pattern.projCount; i++)
            {
                pattern.projDir *= new Vector2(Random.Range(-1f, 1f), Random.Range(-1f, 1f));*

Debug.Log("Randomized dir - " + pattern.projDir*);*
pattern.projAngle = Random.Range(0f, 1f);
Debug.Log(pattern.projAngle*);*

}
}
StartCoroutine(Shoot(pattern, projectile, origin, mod, temp));

}

IEnumerator Shoot(ProjectilePattern pattern, Projectile projectile, Transform origin, string mod, bool temp)
{
for (int i = 0; i < pattern.projCount; i++)
{
//Wait for the delay time.
yield return new WaitForSeconds(pattern.projTimes*);*

//instantiate new projectile and grab reference to it.
var proj = Instantiate(projectilePrefab);

//If the modifier name is valid, Add the modifier to the bullet, else add the default modifier.
if (GameAssets.I.modifiers.Contains(mod))
{
Debug.Log("mod " + mod);
proj.gameObject.AddComponent(Type.GetType(mod));
proj.GetComponent().modifier = (Modifier)proj.GetComponent(Type.GetType(mod));
}
else
{
Debug.Log(“Default mod”);
proj.gameObject.AddComponent();
}

//setting the movement direction of the bullet
var forward = origin.forward;
Vector3 direction = new Vector3(forward.x + pattern.projDir.x, forward.y + pattern.projAngle_, forward.z + pattern.projDir*.y - 1);
proj.transform.position = origin.position + direction.normalized;
Debug.Log("direction = " + direction.normalized);
proj.GetComponent().velocity = direction.normalized;
proj.GetComponent().currentProjectile = projectile;
}*_

if (temp)
{
Destroy(gameObject);
}
}
Its a bit roundabout but i’m quite happy with it overall. Thanks for your help anyways.