Falling collectible bonus on Unity, part 2: The animation

In the last post we created the asset for the animated falling collectible bonus. In case you missed the post read it first here. In this post we use the images and create an animation in Unity for the collectible.

First you need to create a folder under “Assets” in which you will have the images. I have created a folder called “Sprites” and under “Sprites” I have a “Bonus” folder. The structure is up to you but creating a organized structure will help you later when you have more than just few assets (and asset types). Copy the images you created to the Bonus folder. You can import them by using Unity too but I don’t know how to import several images at one command so for me it was easier to use the file system copy.

When files are copied go to the folder and select all the images and drag them to the scene. Dragging the images to scene causes Unity to create an animation to the same folder the images are dragged from, in my case, folder named “Bonus”. You can rename the animation i.e. bonus. At the same time Unity creates a game object with:

  • Sprite Renderer, in which one of the images selected as Sprite (I don’t know is it random or how Unity decides this?)
  • Animator, in which the controller is newly created controller in the same folder. This also seems to have a random name but you can rename it to i.e. bonus. More about the Animator here.

We are going to need also some extra components. We are going to use BoxCollider2D so that we can check if player is colliding with the bonus. Add the BoxCollider2D component to the game object and check the “Is Trigger” check box. This way the bonus only causes trigger when colliding but do not actually collide (interfere with the physics). We don’t want the bonus to bounce or anything from the other game objects. Set the X and Y size so that it matches the sprite boundaries (for me X=0,7 and Y=0,33).

We are going to need also a Rigidbody2D component. You can leave the default values for this component. If the game object do not have Rigidbody2D component it will be ignored by the physics engine. If you are not having this component you need to take care of the movement of the bonus in the scene, plus I think you need to check the collision manually. In my game I wanted the bonus to “fall” so I use gravity to move the object. Your case might be different and require other type of approach.

Now you should have an animation called bonus and controller named bonus in your bonus folder along the sprites and a game object with Sprite Rendered and Animator in which the controller is the “bonus” -controller. You should also have a BoxCollider2D and Rigidbody2D components.

We are also going to add a Script. Add component and select “New Script”, type the name i.e BonusScript and select the type CSharp. This script is going to have the code which is triggered when bonus is collected. The Rigidbody2d has a method:

void OnTriggerEnter2D(Collider2D other) {

// write your bonus handling here or send message to appropriate game object


If you are familiar with the tag concept you can check if the object the bonus is colliding with is a player with this type of clause:

if(other.gameObject.tag == “Player”){


Using gravity increases the speed of the bonus so it’s more realistic, but if you want to move the bonus object same speed all the time then you can use the following script:

void Update () {
gameObject.transform.Translate (Vector3.down * Time.deltaTime);

When you are done with the game object in the scene, drag it from the hierarchy to the folder where you store your prefabs and delete the game object from the scene. This way you can use the prefab to instantiate the actual bonus object in your game.

Until next time, happy coding!