Sharp Coder is reader-supported, meaning when you buy through links on our site, we may earn an affiliate commission.
Unity
/
Controller
/
Player 3D and 2D Wall Jump Tutorial for Unity
/
3D Worm Controller Tutorial for Unity
/
Top-Down Player Controller Tutorial for Unity
/
RTS and MOBA Player Controller for Unity
/
Implementing Parkour System in Unity
/
Dialogue System for Unity
/
Unity How to Make Mobile Touch Controls

Flashlight Tutorial for Unity

In many video games, a flashlight serves as a crucial tool (or gameplay element) that enhances the player's experience. A flashlight is a portable light source that can be turned on and off, typically mimicking the functionality of real-world flashlights. Its importance in games lies in its ability to create suspense, add realism, and provide illumination in dark or dimly lit environments.

Alien Isolation Screenshot

Below is a quick tutorial on how to set up a controllable flashlight in Unity that can be turned on and off with a keypress.

Setting Up The Scene

  • Create a new 3D project in Unity (or open an existing project)
  • Import any necessary assets, such as a 3D character or environment if haven't yet

Create The Spotlight

  • Right-click in the Hierarchy panel and select "Create Empty" to create an empty GameObject.
  • Rename the new GameObject to "Flashlight."
  • Make sure the "Flashlight" GameObject is selected in the Hierarchy.
  • In the Inspector panel, click on the "Add Component" button, search for "Light the click on it to add the Light component to the "Flashlight" GameObject.
  • Configure the Light component to the desired settings, such as setting the Type to "Spot" and adjusting the Range, Angle, and Intensity parameters.

Effect of a Spot Light in the Unity scene

Attach The Flashlight To The Player

  • Drag and drop the "Flashlight" GameObject onto the player character in the Hierarchy panel to make it a child of the player.
  • Adjust the position and rotation of the flashlight so that it aligns with the player's hand or desired position.

Implement Flashlight Controls

  • Create a new C# script by right-clicking in the Assets panel and selecting 'Create -> C# Script', then name it "FlashlightController."
  • Double-click the script to open it in any preferred code editor.
  • Remove the default code and replace it with the following script:

FlashlightController.cs

using UnityEngine;

public class FlashlightController : MonoBehaviour
{
    private Light flashlight;

    private void Start()
    {
        flashlight = GetComponentInChildren<Light>();
        flashlight.enabled = false;
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.F))
        {
            flashlight.enabled = !flashlight.enabled;
        }
    }
}
  • Save the script and go back to Unity.
  • Attach the "FlashlightController" script to the "Flashlight" GameObject.

Test The Flashlight

  • Press the Play button to enter Play mode.
  • Move the character around in the Scene.
  • Press the "F" key to toggle the flashlight on and off.

Conclusion

Hopefully, this tutorial has helped in learning how to create a spotlight flashlight effect in Unity. It can be further enhanced by adding additional features like light flickering or adjusting the spotlight cone angle based on the player's input.