Unity – Adding a Radio, Battery & Ray Casting

Today I added more components to my game, to add a little bit more immersion to the game I added some audio. I did this through the use of a radio. The first step was to insert the model of the radio that was provided. I then downloaded a song from a royalty free website and dragged it onto the radio. Next created a new C# script called RadioController which was attached to the radio. Having just a radio was a bit plain so using the same fundamentals from the door and key I inserted a battery which would need to be picked up before the radio could be activated. The inventory item script needed to be dragged onto the battery and the itemName needed to be changed to Battery.  Other than just items I also changed how the game works slightly; rather than having the player need to use the mouse cursor to click on the items I added in a raycast. This required a new script to be created called RayCaster and this needed to be attached to the camera. The script pretty much creates an invisible line that comes out the camera which means its in the centre of the players view. Because of this change the Inventory scripts needed to be changed to OnRayDown instead of OnMouseDown so it is not activated by the cursor anymore.

RadioController –

radiocontroller

RayCaster –

raycaster

Command Change –

onraydown

radio-battery

 

Leave a comment