Made With Unity | Unity VR Part 9: Flashlight

Let's add a simple flashlight mechanic to our scene so our VR player can interact with it.

Getting The Flashlight

Using Filebase I download a simple flashlight asset and added it to my Unity scene

In Unity, this asset has the following component: Rigidbody, Mesh Collider, and the XR Grab Interactable.

As has been shown in this series of articles, the XR Grab Interactable is the component that is going to allow us to interact with the object in VR.

Creating Light

Add a Spotlight game object as a child to our Flashlight asset

Play around with the Spot Light settings in the Inspector

Flashlight Behaviour Script

Create a new script, named it FlashlightBehaviour, and added it to the Flashlight game object.

On Unity, drag the Spot Light from the Flashlight and place it on the field Flashlight Light OB.

Interactable Events

Back to the Flashlight, locate the XR Grab Interactable component, locate Interactable Events and go down the many events list and find the Activate [Activated(ActivateEventArgs)]

Press the plus sign and drag the Flashlight object to the None (object) empty field. Press where it says No Function and locate the FlashlightBehaviour script.

Select ActivateFlashlight(), is the public method we created on the script.

Press Play

By pressing the trigger, once you are holding the flashlight, you can turn it On or Off the flashlight.

🎮TO BE CONTINUED🎮

--

--

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
Rehtse Studio

Rehtse Studio

Game Developer sharing content around Unity, programing and tips and tricks in game dev.