Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by jaydenpiz · Aug 31, 2012 at 09:34 AM · pickupflashlighthorrorbattery

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!!!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jeuxbastien3311 · Jul 01, 2013 at 12:08 PM 0
Share

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");
 }
 }
 

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

  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! :-)

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jaydenpiz · Aug 31, 2012 at 02:52 PM 0
Share

thank you so much, but how would i go about the on screen gui?

avatar image jaydenpiz · Aug 31, 2012 at 02:53 PM 0
Share

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?

avatar image save · Sep 02, 2012 at 10:34 PM 0
Share

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();
avatar image rickyansar · Jun 03, 2013 at 04:50 AM 0
Share

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

avatar image AngelisDragon · Jun 10, 2013 at 06:43 AM 0
Share

This script will not immediately start draining battery unless you do this:

 function Start () {
 
 ToggleFlashLight ();
 
 }
avatar image
0

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");
 }
 }
 
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rapfan28 · Mar 29, 2015 at 03:16 PM 0
Share

help me please it doesn't work I go near the battery and press e but it doesn't work please help

avatar image Hakobik2016 Rapfan28 · Jun 22, 2016 at 06:44 PM 0
Share

same with me!

avatar image
0
Wiki

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);
     }
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Benproductions1 · Dec 01, 2013 at 12:16 AM 0
Share

Please format your code. If you don't know how watch the tutorial video on the right

avatar image MagicGeek123 · Dec 04, 2013 at 01:45 AM 0
Share

dont see a video to the right?

avatar image Benproductions1 · Dec 08, 2013 at 03:00 AM 0
Share

look harder. It has a giant title labeled "Unity Answers tutorial video!"

avatar image MagicGeek123 · Dec 09, 2013 at 01:09 AM 0
Share

that better?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I need help with my torch/battery pickup script! 1 Answer

Very simple picking up items script? 2 Answers

How would I go about removing a mesh renderer component on collision with a trigger? 1 Answer

Battery Pickup Regeneration 2 Answers

Recharging battery script, help 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges