I have a flashlight component on my player character, and when the flashlight is on, the batterypercentage decreases over time. The battery percentage is is a public float in the code that is a value of 0 - 100. What I am wanting to do is take that value and display a UI bar that fills as charge goes up, and depletes as charge goes down.
Here is the Flashlight script. This script is on two lights that are children of the player object. The script allows for the flashlight to toggle on/off with the F key, causes the batteryPercentage to drain when the flashlight is on, dims the light as the battery drains, and plays a sound when the flashlight is turned on/off.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Light), typeof(AudioSource))]
public class Flashlight : MonoBehaviour {
public AudioClip click;
public float batteryLifeInSec = 300f;
public float batteryPercentage = 100; //this value decreses when the light is on and increases when a battery object is picked up; this is what I am wanting to convert to a UI bar.
public Light lite;
private bool on;
private float timer;
voidStart()
{
lite = GetComponent<Light>();
}
void Update() //this part basically controls the on/off for the flashlight and controls the battery drain based on whether the light is on or off.
{
timer += Time.deltaTime;
if(Input.GetKeyDown(KeyCode.F) && timer >= 0.3f && batteryPercentage > 0) {
on = !on;
GetComponent<AudioSource>().PlayOneShot(click);
timer = 0;
}
if(on)
{
lite.enabled = true;
batteryPercentage -= Time.deltaTime * (100 / batteryLifeInSec);
}
else
{
lite.enabled = false;
}
batteryPercentage = Mathf.Clamp(batteryPercentage, 0, 100);
//the following 21 "if" statements are just adjusting the intensity of the light based off of the batteryPercentage value.
if(batteryPercentage == 0) {
lite.intensity = Mathf.Lerp(lite.intensity, 0, Time.deltaTime * 2);
}
if(batteryPercentage > 0 && batteryPercentage < 5) {
lite.intensity = Mathf.Lerp(lite.intensity, 0.15f, Time.deltaTime);
}
if(batteryPercentage > 5 && batteryPercentage < 10) {
lite.intensity = Mathf.Lerp(lite.intensity, 0.30f, Time.deltaTime);
}
if(batteryPercentage > 10 && batteryPercentage < 15) {
lite.intensity = Mathf.Lerp(lite.intensity, 0.45f, Time.deltaTime);
}
if(batteryPercentage > 15 && batteryPercentage < 20) {
lite.intensity = Mathf.Lerp(lite.intensity, 0.6f, Time.deltaTime);
}
if(batteryPercentage > 20 && batteryPercentage < 25) {
lite.intensity = Mathf.Lerp(lite.intensity, 0.75f, Time.deltaTime);
}
if(batteryPercentage > 25 && batteryPercentage < 30) {
lite.intensity = Mathf.Lerp(lite.intensity, 0.9f, Time.deltaTime);
}
if(batteryPercentage > 30 && batteryPercentage < 35) {
lite.intensity = Mathf.Lerp(lite.intensity, 1.05f, Time.deltaTime);
}
if(batteryPercentage > 35 && batteryPercentage < 40) {
lite.intensity = Mathf.Lerp(lite.intensity, 1.2f, Time.deltaTime);
}
if(batteryPercentage > 40 && batteryPercentage < 45) {
lite.intensity = Mathf.Lerp(lite.intensity, 1.35f, Time.deltaTime);
}
if(batteryPercentage > 45 && batteryPercentage < 50) {
lite.intensity = Mathf.Lerp(lite.intensity, 1.5f, Time.deltaTime);
}
if(batteryPercentage > 50 && batteryPercentage < 55) {
lite.intensity = Mathf.Lerp(lite.intensity, 1.65f, Time.deltaTime);
}
if(batteryPercentage > 55 && batteryPercentage < 60) {
lite.intensity = Mathf.Lerp(lite.intensity, 1.8f, Time.deltaTime);
}
if(batteryPercentage > 60 && batteryPercentage < 65) {
lite.intensity = Mathf.Lerp(lite.intensity, 1.95f, Time.deltaTime);
}
if(batteryPercentage > 65 && batteryPercentage < 70) {
lite.intensity = Mathf.Lerp(lite.intensity, 2.1f, Time.deltaTime);
}
if(batteryPercentage > 70 && batteryPercentage < 75) {
lite.intensity = Mathf.Lerp(lite.intensity, 2.25f, Time.deltaTime);
}
if(batteryPercentage > 75 && batteryPercentage <= 80) {
lite.intensity = Mathf.Lerp(lite.intensity, 2.4f, Time.deltaTime);
}
if(batteryPercentage > 80 && batteryPercentage <= 85) {
lite.intensity = Mathf.Lerp(lite.intensity, 2.55f, Time.deltaTime);
}
if(batteryPercentage > 85 && batteryPercentage <= 90) {
lite.intensity = Mathf.Lerp(lite.intensity, 2.7f, Time.deltaTime);
}
if(batteryPercentage > 90 && batteryPercentage <= 95) {
lite.intensity = Mathf.Lerp(lite.intensity, 2.85f, Time.deltaTime);
}
if(batteryPercentage > 95 && batteryPercentage <= 100) {
lite.intensity = Mathf.Lerp(lite.intensity, 3, Time.deltaTime);
}
}
}
The other component which interacts with this and give recharge is a battery object which you pick up by walking over with the player character. Including this in case it is needed to address the question or if anyone is interested in using this code snippet for their own projects. Keep in mind that I am very new to this so while I have made changes to these code snippets for use with my game, credit goes to people on these forums for the base code. Thanks in advance for anyone who is willing to help me figure this out. Learning every day thanks to this awesome community! (the following code goes on the battery object)
using UnityEngine;
using System.Collections;
public class Battery : MonoBehaviour {
voidStart() //automatically assigns a sphere collider with trigger to the object
{
this.GetComponent<SphereCollider>().isTrigger = true;
}
void OnTriggerEnter(Collider other) //check to see if object it is colliding with is tagged "Player", then looks to see if children of the player have a component named "Flashlight> and restores 50 batteryPercentage to each light that is a child of the player that has a Flashlight component.
{
if(other.gameObject.tag == "Player")
{
foreach (varflashlightinother.GetComponentsInChildren<Flashlight>())
{
flashlight.batteryPercentage += 50;
}
}
}
}
I also have this snippet on my character movement script. I’m not sure if this should be in a different place, but all it does is destroys (actually just turns SetActive to false) pick up objects on collision and plays a sound for the pick up.
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
GetComponent<AudioSource>().PlayOneShot(collect);
other.gameObject.SetActive (false);
}
}