Tutorial: Collectables - CodingDino/Dino-Unity-Toolkit GitHub Wiki

In this tutorial, you will learn how to create collectables that get "picked up" when the player touches them. In this case, we'll use coins as an example. We'll accomplish picking up the coins by destroying them when the player touches them.

This tutorial requires a moving player using something like the Tutorial: Platformer Player Movement. It also requires a coin sprite with a 2D collider attached to it. Example assets are provided in the tutorial package.

Before we start, let's clearly state our goal behaviour:

"Pick up" the coin touched whenever the player touches the coin.

Action

In the stated goal behaviour above, the first part of the sentence describes the Action that should occur:

"Pick up" the coin touched...

Our action is to pick up the coin. We accomplish this by destroying the coin GameObject in the scene, which will cause it to disappear. This is done using a DestroyObjectAction component. This component is used to destroy a specified GameObject in the scene.

Drag the coin sprite into the scene and attach a 2D collider to it, such as a CircleCollider. On the collider component, check the "Is Trigger" box to ensure our coin won't make the player stop when the player runs into it.

Now add a DestroyObjectAction component to the coin. This will allow it to be destroyed when touched by the player.

The action is now all ready to use - but as always, it must be triggered by an Activator in order to actually happen.

Activator

In the stated goal behaviour above, the second part of the sentence describes the Activator condition:

...whenever the player touches the coin.

So, we want an activator that will do something when the coin is touched by a specific object. For this, we'll use the OnTrigger2DActivator. We use the "Trigger" activator instead of the OnCollision2DActivator because we don't want the coins to block the player's movement.

Add the OnTrigger2DActivator component to your coin.

You'll notice this activator has some condition fields. We'll use those to make sure we only destroy an object that we touch if it is, in fact, the player. We wouldn't want the coin to be destroyed when other things, like enemies, touch it!

Check the "Check Tag" box and type "Player" into the "Target Tag" field that appears. This means that the Activator will only trigger if the thing that bumped into the player has the "Player" tag. Note that this must match EXACTLY with the spelling and capitalisation for the tag on the object in order to work.

We should add the "Player" tag to our player - otherwise our Activator will never trigger the Action! Drag your player into the scene if they are not already present. Select your player and click the "Tag" dropdown in the top left of the inspector.

Select the "Player" tag from the list.

Now our conditions are all set up. All we need to do is add the actual connection between our Activator and our Action.

Click back on the coin. Add a new action to the "On Trigger Enter Actions" list. Drag the coin GameObject into the GameObject slot for the action, then open the dropdown. Select DestroyObject > ActionDestroyObject.

IMPORTANT: Do NOT choose the "Dynamic" version of this action - at the top of the list. The Dynamic version will pass information from the Activator to the Action. In this case, it would pass along what GameObject should be destroyed - namely, the one we bumped into - our player! We don't want to destroy the player when they touch the coin, we want to destroy the coin, so we have to choose the non-dynamic ("Static") version of the Action.

After you choose the Action from the list, you will be given another box. Drag the coin GameObject into this box - this is telling the Action what GameObject should be destroyed, and in our case, that is the coin.

Testing

If you did the above steps correctly, you should now be able to run your scene, move your player onto the coin, and the coin should disappear. Try it out!

Next up, try adding a scoring system to your coins in the Tutorial: Score!