Instantiating Animations.

Jason Schneider
3 min readAug 10, 2021

Ok, rather than attaching an Animation to an object, maybe we can instantiate the Animation at the object we want to animate.

In order to accomplish this, we need to take the first sprite of our Animation and drag it into the hierarchy, click on it and create and save the Animation to it as usual. Next we turn this Animation into a prefab, by dragging it into our prefab folder and renaming it let’s say _explosionPrefab.

In this instance I want an explosion animation to instantiate at an Asteroid so it seems as though the Asteroid is destroyed and doesn’t just disappear. First we will need to add the Asteroid sprite to the scene and adjust its size to our liking.

We will also need to add a rigid body and collider. Then we will need to add a script to the Asteroid. We can add a rotation to the Asteroid by first adding a variable:

SerializeField so we can adjust it in the Inspector.

After which we can add:

to the Update method, saving the script and jumping back into Unity and playing will give:

Next, in order for the Asteroid object to know about our Animation, we need to add another variable:

Saving the script and jumping back into Unity we then need to drag our _explosionPrefab into the newly created field.

In order to have the explosion Instantiate at our Asteroid when we hit it with a laser, we will need to add the following to our script:

Destroy(this.gameObject, 0.2f), after 0.2s destroy Asteroid.

Saving the script and jumping back into Unity and running game will give:

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response