In the last article, we got more advanced with animations in Unity by learning about how to use in-game events to trigger animations. In this article, we’ll continue to increase our Unity animation skills by learning how to combine sprites and animations into unified game objects.
In an earlier article we combined multiple game objects to build a more complex composite object. We can follow a similar process to create a composite object from multiple static and animating sprites.
Why would we want to do that though? Well, consider a situation in a game where the player or an enemy may have an effect on them that is not part of them. Perhaps they are surrounded by a force field or an aura of some sort.
The benefit of combining them as a composite object is that we are able to manipulate (move, rotate, scale, etc) all of the parts together at once, instead of having to do each individual object separately. Let’s get started.
At this point we should determine what we consider to be the “base” object. What do I mean by that? Well, let’s think of a possible video game example. Maybe we have a static space ship sprite and an animated, shimmering force field. We want the force field attached to the ship and to move with the ship.
But maybe the field won’t always be around? Perhaps the player needs to acquire a powerup to get it in the first place? Maybe the enemies can bring it down? In this example, the space ship would obviously be the base object.
In my own example, with an asteroid and a flame effect, I am selecting the asteroid as the base object. Make a determination with your own assets and we can proceed to the next step.
Within the Hierarchy window, drag all the other desired parts to the base object, making them children to that object. Then position all of the child objects how you want them relative to the base object.
It may be necessary to make adjustments to the sorting layers of the various objects in order to get things looking the way you want. This can be done by selecting the individual object and finding the Sprite Renderer component in the Inspector window.
That’s basically all there is to it. Once the objects are put into a relationship with each other, they act in unison.
Also, relating back to the idea that the child objects could be temporary effects, we can selectively enable or disable the child objects whenever we wish without impacting the base object.
Going back to the force field example, we could enable the force field object when the player picks up a powerup and disable it when enemies hit the player. And we can do that without disrupting the space ship object.
That does it for animation for the time being. In the next article, we’ll be introducing ourselves to the subject of post processing in Unity. Until then, good luck and happy coding!