Creating Coin Distraction Feature in Unity (Part 1)

Instantiate a coin on player click position

Objective: Instantiate a coin prefab on player right click position.

In this part we are going to create a code to instantiate a coin prefab when the player makes a right click and play the drop coin audio.

In the player script we have to create a variable to store the coin prefab and assign it in the inspector.

I added an audiosource component to the coin prefab and assigned the drop coin audio to it. Also I left the play on awake property checked.

Last thing is in the script player check for the right click, then create a ray from the mouse click position and if the raycast detects the floor we instantiate the coin prefab in the hit point position.

And that’s it!!! When the player makes a right click, a coin prefab is instantiated in the click position.

A Software Engineer passionate about game dev and interactive products with Unity. I consider video games to be the artistic expression of programming.