- [Narrator] So now that we have a simple level and our player, let's take a look at building a collectible item so that our player can pick something up. Let's go into our Jetroid artwork folder and inside of sprites go to collectibles and drag the first crystal into the scene. Let's double click on it inside of the hierarchy view to get a closer look and zoom out. Let's also rename this to crystal and remove the frame number, and then inside of our Jetroid folder, let's drag the crystal into the prefab folder so we can save it as our second prefab.
Next, we're going to want to add a box collider to it so that we can detect when the player and the crystal collide. From the inspector select add component and type in box collider and select the box collider 2D. You should see a green outline around the sprite now inside of our scene. One thing that we're going to do though is we're going to change our box collider to a trigger. A trigger allows us to detect for a collision, but it won't actually affect the two objects colliding.
So the player will be able to walk through the crystal, and we'll still be able to detect when the collision has happened. Let's create a new script in order to store the logic for our new collectible. Inside of our Jetroid scripts folder let's right click and create a new C sharp script. We'll call this collectible. And once it's created, open it up inside of mono develop by double clicking on it. Now that we have our new collectible script open, let's go ahead and create a new private method called void on trigger enter 2D.
This will get called whenever a trigger event happens inside of our collider. This is only called when a box collider 2D is set to trigger. This method will accept a collider 2D, and we're going to call this target. Now this will be triggered anytime another box collider or collider 2D enters the collectible. What we want to do is make sure that only the player can pick up the object.
We'll use a condition to test that the target game object has a tag that's equal to player. If this is true, we'll call destroy on the game object in order to remove the collectible from the scene. Now let's save this and go back into Unity. We can drag the collectible script onto the crystal. And let's select a player and change the tag of the player.
Game objects allow you to change a tag and a layer to help you do more find tuning of collision and to group similar objects together. Let's go ahead and click on the tag dropdown, and you'll see that player has already been created automatically as part of the default list of tags inside of Unity. Select player, and let's run our game and see what happens. Here you'll see the crystal, and when the player walks over it, the crystal gets destroyed.
If you stop the game, if you select the crystal, you may want to move the crystal down closer to the platform since it doesn't have a rigid body, it doesn't adhere to gravity. Select the move tool and drag the crystal down. Now when you rerun the game, the crystal will be on top of the platform, and now the player can walk over the crystal. Let's stop the game, and now we have a working crystal and the player that can collect it. Let's go ahead and save our scene.
- Optimizing sprites and draw cells
- Cutting up sprite sheets
- Building animations
- Changing animations
- Creating a player
- Moving the player
- Building a level with a simple map
- Creating interactive objects
- Creating alien enemies
- Switching between scenes