This script can be used to create a "flashlight" effect, where a light is shone in the scene where the cursor is pointing.
To use it:
- Create a new C# file named CursorLight.cs, and paste in the code below
- Create a Light in your scene (Spotlight if 3D, Point Light if 2D) and attach the new Cursor Light component
- Configure the Inspector, setting the light's default's state, and whether it's 3D or not.
- To turn on and off at runtime, use the Object: Send message Action to pass Turn On and Turn Off commands to the Light object
CursorLight.cs:
using UnityEngine;
using AC;
public class CursorLight : MonoBehaviour
{
public float depth = 2f;
public bool isOn = true;
public bool is3D = true;
public void TurnOn ()
{
isOn = true;
}
public void TurnOff ()
{
isOn = false;
}
void Update ()
{
if (isOn)
{
Vector3 mousePosition = KickStarter.playerInput.GetMousePosition ();
mousePosition.z = depth;
Vector3 mouseWorldPosition = KickStarter.CameraMain.ScreenToWorldPoint (mousePosition);
if (is3D)
{
transform.position = KickStarter.CameraMain.transform.position;
transform.forward = mouseWorldPosition - transform.position;
}
else
{
transform.position = mouseWorldPosition;
}
}
else
{
transform.position = new Vector3 (-1000f, 0f, 0f);
}
}
}