Prefab Follow Effect

Hey all. So I figured something like a Healing Potion would want more than just a prefab to spawn at the player’s feet and then stay there when you run away. So I made a simple change to the Prefab Spawn Effect. I am actually amazed it worked (So I am waiting for Brian to tell me what bugs I introduced doing this). Now you can tell it to stick the prefab on the user’s transform so it stays with them. I suppose that you could just make a separate prefab spawn effect for this, but it’s just one field and one if statement to adjust the current one. There are PLENTY of abilities that this will be handy for(buffs and MOST self targeted abilities in general):

public class SpawnTargetPrefabEffect : EffectStrategy
{
    [Tooltip("Prefab to Spawn.")]
    [SerializeField] Transform prefabToSpawn;
    [Tooltip("Should the prefab be parented to the user?")]
    [SerializeField] bool followUser = false;
    [Tooltip("How long before destroying Prefab? (-1 for never).")]
    [SerializeField] float destroyDelay = -1;

    public override void StartEffect(AbilityData data, Action finished)
    {
        data.StartCoroutine(Effect(data, finished));
    }

    private IEnumerator Effect(AbilityData data, Action finished)
    {
        Transform prefabInstance;

        // Spawn prefab on the user if enabled in editor, otherwise spawn at the selected target with no parent.
        if (followUser) prefabInstance = Instantiate(prefabToSpawn, data.GetUser().transform);
        else
        {
            prefabInstance = Instantiate(prefabToSpawn);
            prefabInstance.position = data.GetTargetedPoint();
        }

        if(destroyDelay > 0)
        {
            yield return new WaitForSeconds(destroyDelay);

            Destroy(prefabInstance.gameObject);
        }
        finished();
    }
}
3 Likes

Nice and simple.
As an added bonus you could change the effect to simulate in world space (if it’s not already) to make it look a little better when you move. The particles won’t move with the particle system when the player moves.

I made a quick video to show the difference between Local Simulation Space and World Simulation Space

The option is here
image

2 Likes

Yep!

The effect I was using for healing potion is mostly just a summon circle type effect so it works with local just fine, but good looking out. I probably would have forgotten about world space for fancier long lasting buffs!

Actually, I take that back. I went ahead and swapped this one to world space and it looks even slightly better with some of the residual particles. I was worried the summon circle itself would look off but it still follows perfectly.

Privacy & Terms