- Home /
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!!!
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 ins$$anonymous$$d
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");
}
}
Answer by save · Aug 31, 2012 at 11:09 AM
What you basically have to do is break it into a couple of simple steps.
Your flashlight uses power - when it's turned on it drains energy.
You can replenish the energy by picking up batteries.
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! :-)
thank you so much, but how would i go about the on screen gui?
Hey is there a way to not have the torch acctivated, then find an object of the torch on the ground, and if i press 'e' then i can use the torch?
For the GUI part you would either use the built-in OnGUI, GUI-textures or regular GameObject(s). For instance have a look here: http://answers.unity3d.com/questions/155842/working-health-bar-amp-gui.html
Your torch problem could be solved in a similar way as the battery pickup. For instance, the player could have a boolean named hasTorch. When the player picks up that part (with OnTriggerEnter), set the player's variable hasTorch to true. Then check if hasTorch is true before you let the player activate it.
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E) && hasTorch) ToggleTorch();
Alterative energy error keeps poping up for me. What do I do? Like I collect the battery then it crashes saying alt energy not found
This script will not immediately start draining battery unless you do this:
function Start () {
ToggleFlashLight ();
}
Answer by jeuxbastien3311 · Jul 01, 2013 at 12:19 PM
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");
}
}
help me please it doesn't work I go near the battery and press e but it doesn't work please help
Answer by MagicGeek123 · Nov 30, 2013 at 10:09 PM
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);
}
}
Please format your code. If you don't know how watch the tutorial video on the right
look harder. It has a giant title labeled "Unity Answers tutorial video!"