Parenting an animation to an effect? (unity)



Hey guys!
I’m working on my first effect and I have a trail of sparks that comes off a sword. I have an attack animation for the character, and my particle system already follows the motion of the sword (placed it under that in the hierarchy). My question is, can I put my effect to play as part of this animation? I want to work on another effect involving the same character in a different animation, so I wanted to know if it’s possible and how I could go around assigning an animation to play a specific effect.


This can probably be done in many ways but one way would be to trigger the VFX using Animation events.
Using Animation Events you’d need a small script that’s attached to the Character (on the same GameObject where the animation component is located) that reference your VFX GameObject. Then you write a function in that script that enables that VFX (and also probably disables emission after X seconds)
Then you call that function on the Animation Event in the animation where you want to trigger the VFX.
For example you can have one function called “SwordSwoosh” and another one called “Emote” that reference different VFX GameObjects.

Useful tip: You can leverage the ParticleSystemStopAction to let the particles manage themselves and just call particleSystem.Stop() when they should stop. If stop action (on the actual particle system) is set to Disable the particle GameObject will be disabled when all particles are expired (not destroyed)

Also useful: This method does not scale well in production, you’d need a proper VFX manager for that :wink:


Another variant:

If you use Animator for control animations, you can write script inherited from StateMachineBehaviour that will be use functions of some VFXManager or just instantiate/detete prefab on start/end of animation state in animator or anything else. Just add this component for animation state, it will be done every time when you reach this state. So you can place it on all animation states where you need it, but It has a little confuse when it work with transitions(Your current state ends after starting of new one) so be careful.

This script can`t keep refs from scene, only refs of assets. So, its great for instantiating prefab or for work with some VFXManager, that will do everything what you need by one/two refs(VFX prefab ref/object ref(animator variable in this case), which need turn on VFX or both)

P.S. Well, you can get and keep refs in real-time from animator variable(.getComponent() for example) on start of state, but not recommend keep refs in this case for memory/perfomance issues. VFXManager best choice :slight_smile: