Creating Coin Distraction Feature in Unity (Part 1)

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.



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
Rusben Guzman

Rusben Guzman

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