Flashlight pickup, battery etc

Hey guys, in my horror game im making, i need to have a flashlight that has battery, that can run out, and replenish the battery. the flashlight script and how to have it so i can start off without it and find it and pick it up. ive had an attempt myself but i keep getting errors and so on. ive looked areound unity answers aswel and no luck for me, all help is greatly appreciated as i would do the same for you guys!!!

What you basically have to do is break it into a couple of simple steps.

  1. Your flashlight uses power - when it’s turned on it drains energy.
  2. You can replenish the energy by picking up batteries.
  3. When your flashlight runs out of power the light source turns off.

An easy way to attack this is to have a float variable which represents the energy, connect this to a light source, a function for drainage and pickups that are trigger colliders.

I haven’t tested this out but the script logic could be done like this,

//Name this script Flashlight and attach it to your player for instance

var lightSource : Light; //Connect the light source in the Inspector
static var energy : float = 100; //The energy amount of the flashlight
static var turnedOn : boolean = false; //Boolean to check whether it's turned on or off
private var drainSpeed : float = 2.0; //The speed that the energy is drained

function Update () {
	if (Input.GetKeyDown(KeyCode.F)) ToggleFlashlight();
}

//When the player press F we toggle the flashlight on and off
function ToggleFlashlight () {
	turnedOn=!turnedOn;
	if (turnedOn && energy>0) {
		TurnOnAndDrainEnergy();
	} else {
		lightSource.enabled = false;
	}
}

//When the flashlight is turned on we enter a while loop which drains the energy
function TurnOnAndDrainEnergy () {
	lightSource.enabled = true;
	while (turnedOn && energy>0) {
		energy -= drainSpeed*Time.deltaTime;
		yield;
	}
	lightSource.enabled = false;
}

//This is called from outside the script to alter the amount of energy
static function AlterEnergy (amount : int) {
	energy = Mathf.Clamp(energy+amount, 0, 100);
}

Then for the pickups, you add a collider which is set to trigger. When the player intersects with the collider we trigger the energy event.

//Put this script on the pickup

var batteryPower : int = 10; //The amount of battery power to give, negative value hogs energy instead

function OnTriggerEnter (other : Collider) {
	if (!other.CompareTag("Player")) return;
	Flashlight.AlterEnergy(batteryPower);
	Destroy (gameObject);
}

The example should handle if you want to instantly drain the power by a pickup too, having a pickup that is stealing power for instance. What you probably want to do after this is to show the amount by some sort of GUI. You would then take the length of the GUI times the current amount of energy divided by the maximum energy. Length = maximumLength*currentEnergy/maximumEnergy.

Hope it’s helpful for you!
When asking for help in the future it helps out a lot to see your current script attempts, so you get help with the actual implementation. Just as a reminder! :slight_smile:

to take the battery earth by pressing’’ E’’ instaler trrigger the battery in a box and insert the script here the script, add the Input name e.

//Put this script on the pickup

    var batteryPower : int = 80; //The amount of battery power to give, negative value hogs energy instead
   
    var message : boolean = false;
   
   function OnTriggerStay(other : Collider)
    {
       if(Input.GetButton("e")){
     
     Flashlight.AlterEnergy(batteryPower);
     
          Destroy(gameObject);
     
       }

     
    }
     function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == "Player") {
message = true;
}
}

function OnTriggerExit (other : Collider){
if (other.gameObject.tag == "Player") {
message = false;
}
}


       function OnGUI(){
if(message){
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 300, 30), "Appuyer 'E' pour prendre les battery");
}
}

try this it also has capability for a gui that shows how much battery you have

#pragma strict
var tex1 : Texture; //100
var tex2 : Texture; //75
var tex3 : Texture; //50
var tex4 : Texture; //25
var tex5 : Texture; //0

var maxBatteryLife: float = 100.0f;
var batteryLifeLostPerSecond: float = 3.0f;
var linkedLight: Light;
	
var currentBatteryLife: float ;
	
	function Start() 
	{
		currentBatteryLife = maxBatteryLife;
		linkedLight.enabled = false;
	}
	
	function Update() 
	{
		if (Input.GetKeyDown("f")) 
		{
			linkedLight.enabled = !linkedLight.enabled; // Toggles the light
		}
		
		if (linkedLight.enabled) 
		{
			currentBatteryLife -= batteryLifeLostPerSecond * Time.deltaTime; // Reduces the battery correctly over time
		}
		
		if (!linkedLight.enabled) 
		{
			currentBatteryLife += batteryLifeLostPerSecond * Time.deltaTime; // Increases the battery correctly over time
		}
		
		if (currentBatteryLife <= 0) 
		{
			linkedLight.enabled = false; // Keeps the light off when there is no power.
		}
	}
	
	function OnGUI ()
{
	if(currentBatteryLife >= 75)
	{
		GUI.DrawTexture(Rect(Screen.width / 18.5, Screen.height* 0.8 , 44, 60), tex1);
	}
	
	if(currentBatteryLife >= 50)
	{
		GUI.DrawTexture(Rect(Screen.width /18.5, Screen.height* 0.8  , 44, 60), tex2);
	}
	
	if(currentBatteryLife >= 25)
	{
		GUI.DrawTexture(Rect(Screen.width / 18.5, Screen.height* 0.8, 44, 60), tex3);
	}
	
	if(currentBatteryLife >= 1)
	{
		GUI.DrawTexture(Rect(Screen.width / 18.5, Screen.height* 0.8 , 44, 60), tex4);
	}
	
	if(currentBatteryLife >= 0)
	{
		GUI.DrawTexture(Rect(Screen.width / 18.5, Screen.height* 0.8 , 44, 60), tex5);
	}
}