Part 19 - Deleting Coins.
At present, all the coins at just falling into infinity. It'd be nice if they were captured and deleted.
As a precursor to creating a game, let's add a little area below the floor
object as a coin collection area.
I'm going to leave this as an exercise for the reader now. But essentially it's going to need to be a minimum of a block, the same width as the floor. Just below the floor and with no gaps.
You can add another material if you want.
Here's mine…
It's the selected dark object. I named it collection
.
Git respository: https://bitbucket.org/hiveit/unity-tutorial-coin/src/014_AddedCollectionBasicObject/
We're going to listen to objects colliding with this and destroy them, essentially.
Add a Component script to the collection
object. DeleteCollidingObject
.
We are going to add a new Event function, OnCollisionEnter
.
The default outline looks like
private void OnCollisionEnter(Collision other)
The parameter type of Collision
which is the object that just collided with this object. It has a property of gameObject
which is a reference to the object itself. So we can destroy it.
So this function, this class, in full looks like
public class DeleteCollidingObject : MonoBehaviour {
private void OnCollisionEnter(Collision other) {
Destroy(other.gameObject);
}
}
And with that we end up with
Git respository: https://bitbucket.org/hiveit/unity-tutorial-coin/src/015_DeleteCoins/
More posts in this series.
- Part 01 - Installation
- Part 02 - Concepts
- Part 03 - Navigation
- Part 04 - Git and Unity
- Part 05 - Building a project
- Part 06 - Adding some plugins
- Part 07 - Creating some objects
- Part 08 - Physics
- Part 09 - Pushing things & animation
- Part 10 - Pushing coins
- Part 11 - Custom Scripting
- Part 12 - Spawning things
- Part 13 - It is still too fast
- Part 14 - Coins are piling up
- Part 15 - Materials
- Part 16 - User control
- Part 17 - Keyboard input - Fire
- Part 21 - Adding a GUI
- Part 22 - Adding the GameController
- Part 23 - Scoring links with events
- Part 24 - Refining the Game - Part 1
- Part 25 - Refining the Game - Part 2
- Part 26 - Refining the Game - Part 3
- Part 27 - More refinements
- Part 28 - Slowing the user down
- Part 29 - Some more changes