- Home /
battery pickup
Hi guys im making a hrror game and need a battery pickup.i am using this script for flashlight.
var lightSource : Light; //Connect the light source in the Inspector static var maxEnergy : float = 100; //The energy amount of the flashlight private var currentPower; var turnedOn : boolean = false; //Boolean to check whether it's turned on or off var drainSpeed : float = 2.0; //The speed that the energy is drained private var alpha : float;
private var duration : float = 0.2;
private var baseIntensity : float; var rectHeight: float;
function Update () { if (Input.GetKeyDown(KeyCode.F)) ToggleFlashlight(); if(currentPower < maxEnergy/4 && lightSource.enabled){ var phi : float = Time.time / duration 2 Mathf.PI; var amplitude : float = Mathf.Cos( phi ) * .5 + baseIntensity; lightSource.light.intensity = amplitude + Random.Range(0.1, 1.0) ; } lightSource.light.color = Color(alpha/maxEnergy, alpha/maxEnergy, alpha/maxEnergy, alpha/maxEnergy); alpha = currentPower;
if (turnedOn==true) {
if(currentPower > 0.0) currentPower -= Time.deltaTime * drainSpeed;
if(currentPower <= 0.0) {lightSource.enabled = false;}
}
if (turnedOn==false) {
if(currentPower < maxEnergy) currentPower += Time.deltaTime * drainSpeed/2;
}
}
//When the player press F we toggle the flashlight on and off function ToggleFlashlight () { turnedOn=!turnedOn; if (turnedOn && maxEnergy>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 && maxEnergy>0) { maxEnergy -= 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) { maxEnergy = Mathf.Clamp(maxEnergy+amount, 0, 100); }
//Display current battery on your flashlight function OnGUI () { GUI.Label (Rect(70, Screen.height/rectHeight - 75,150,60), "Battery: " + maxEnergy.ToString("F0") + "%"); }
and i tried all the battery pick up i counld find but all didnt work.i attached to a cube and made sphere collider but i just walked through it.
Hey, just a little heads up cause I have the problem sometimes posting a question. Sometimes the [ code ] tags are pretty weird. I'd edit it and paste them in again, it looks really messy and can be quite hard to read
Can you rephrase your question please? I'm having trouble understanding the problem.
Answer by OtsegoDoom · May 13, 2014 at 01:26 PM
So your problem is that your battery isn't doing anything when you "collect" it? You need to make sure you actually have code that detects when the player has touched the battery collectable.
function OnTriggerEnter (other : Collider) {
if (other.tag == "Player") {
//Add your code to do whatever it is you want to do with the battery
}
}
Put this script on your battery. Make sure the collider on your battery is set to be a trigger. The player would need to have a rigidbody for OnTriggerEnter to work, and would need to have the tag "Player".
Your answer
![](https://koobas.hobune.stream/wayback/20220613144929im_/https://answers.unity.com/themes/thub/images/avi.jpg)