Setting Up a Collectable Diamonds System (2): Enemy adjustments

Following on from the previous article, we’ll need to make some adjustments to our enemies in order for them to drop/spawn diamonds after dying. Since each of enemies inherit from the Enemy base class, we do in fact only need to write out this logic in one place for it to be implemented across the board.

Essentially, the Die/Death method will need to call a “SpawnDiamond” method before Destroy is called. This method will instantiate a diamond at the position of the enemy, access its Diamond script, then set its value using that public Diamond.SetDiamondValue method we created previously.

For a bit of extra flare, although this is only if you’re using a RigidBody2D on the diamonds, we can apply an upwards force to the newly-instantiated diamond. We can do this using the RigidBody2D.AddForce method, as well as UnityEngine.Random.Range to randomise the force applied. I personally settled on a couple of hard-coded values (100f and 150f) but do feel free to experiment 😉

With this all ready, head back into Unity, let it compile, and ensure each of your enemies has the diamond prefab specified in their serialised “diamondPrefab” GameObject variable. And with that, you should be good to go! 😄

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Marcus Ansley

Marcus Ansley

Game Developer | Game Design and Literature Graduate