Created
February 12, 2022 19:38
-
-
Save kurtdekker/cfdf56b5c6cd45619b1a3e278560384e to your computer and use it in GitHub Desktop.
Simple flashlight script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
// @kurtdekker | |
// place this on the flashlight, which must have a Light component | |
public class Flashlight : MonoBehaviour | |
{ | |
public float battery = 20; | |
bool isOn; | |
void Update() | |
{ | |
if (Input.GetKeyDown("f")) | |
{ | |
isOn = !isOn; | |
} | |
if (isOn) | |
{ | |
battery = battery - Time.deltaTime; | |
} | |
if (battery <= 0) | |
{ | |
isOn = false; | |
} | |
GetComponent<Light>().enabled = isOn; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment