Setting Up a Collectable Diamonds System (1): Setting up the diamond

Marcus Ansley
2 min readSep 26, 2021

--

Setting up the collectable diamonds system is a bit of a multi-stage process so I’ll split this into a short series of articles 😉 Let’s start by taking a look at setting up the Diamond prefab and class:

Diamond Prefab

First, create a 2D Sprite GameObject, then assign a “Diamond” sprite to it. If you have a series of images to use in animating this, create an AnimatorController and Animation and hook these up until you’re happy with the result. Then add this controller to an Animator component on the prefab. In my case, this gives the diamond a neat little shimmer effect.

This’ll need at least one type of 2D collider with isTrigger set to true in order for the collection system to work, but you could also choose to add a second collider (for instance A PolygonCollider2D) with isTrigger set to false and a custom PhysicsMaterial2D applied to it. Couple this with a RigidBody2D and you can have these diamonds fall and bounce a little if you’d like (but do feel free to have them just stay in mid-air if you prefer 😉).

Lastly, create and add a “Diamond” script to the prefab. Double click the script and let’s delve into that now:

Diamond Class

The main methods this’ll need is an OnTriggerEnter2D method that’ll update the player’s Gems/Diamonds property, as well as a public “SetDiamondValue” method we’ll be able to call from another script (for instance, one of the enemies) in order to change the value of the diamond. Again, this is up to you, but we can also have some sort of “SetScale” method that’ll add some variation to the size of the diamond depending on its value:

Once the Player’s gems/diamonds have been updated, we can then safely destroy this Diamond. As you can see, I’m also calling a method on a “GameEvents” script before doing so, but I’ll touch on that in an article or two’s time 😉

--

--