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
0
Question by wololo · Jan 08, 2013 at 01:13 AM · flashlightbarbatteryindicator

Flashlight battery indicator

Hi, as I said in my previous question, I'm new to Unity. I'm making a slender rip-off (yes, antoher slender question). I'm having trouble to make a bar that displays how much battery is left on the flashlight. I have an indicator, but it is in numbers so I want it to be a bar muck like a health bar

If anyone's interested, here's the script for the battery indicator I have at the moment:

pragma strict

//Makes you write with stricter formatting, which in turn increases the efficiency of the script

//Batteries

var batteries : int = 0;

//How much charge per battery

var charge : float = 100;

private var chargeDisp : float = 1;

/*^^ this is just used to display the variable through GUI.Label -- when a (Long) variable changes

many times, very fast, unity cannot keep up with redrawing the variable at that speed,

and so the text flashes. I don't like the look of it. This converts it down

a couple of decimal places, which makes it easier on the engine. I like how it looks at 2

decimal places. Convert it to 1, for no decimal places, and 100 for two decimal places.

1 is the number, 0 is a decimal place*/

//The actual torchlight object

var torch : Light;

//Speed at which the light decreases

var speed : float = 1;

//Used to turn light on or off

private var lightState : boolean = false;

function Awake() //Called before anything in a script, even called before start

{

torch.intensity = 1.85; //Set the intensity to 1 (1 is defined as 100 charge)

}

function Update ()

{

 chargeDisp = Mathf.Round(charge * 100) / 100; //See line under variable declaration

 

 if(lightState && charge > 0) //If mouse has been pressed, and there is still charge in the battery

 {

     torch.enabled = true; //Turn on the torch

     charge -= Time.deltaTime * speed; //Decrease the charge

 } else { //If one of the terms has not been met (IE, mouse isn't pressed or there is no charge

     torch.enabled = false; //Turn the torch off

 }

 

 if(charge <= 0 && batteries > 0) //If charge is less or equal to 0, and if batteries is above 0

 {

 batteries = batteries -1; //Take on from the batteries

 lightState = !lightState; //Toggle lightState

 charge = 100; //Charge equals 100 (This simulates putting another battery in the torch. 1 less battery, full charge

 }

 

 if(batteries == 0) //If there is no batteries

 {

 charge = 0; //There is no charge. This stops 'Batteries 1', 'Charge 100' from happening

 }

 

 if(Input.GetMouseButtonDown(1)) //Called when the mouse right click is down

 {

     lightState = !lightState; //Toggle lightState

 }

 

 if(charge < 4) //If charge is less than 5

 {

     torch.intensity = charge; //The torches intensity equals its charge. This makes the torch brighter, but then it fades out pretty quickly

 }

}

function OnGUI()

{

 GUI.backgroundColor = Color.green;
 GUI.Button (Rect (10, 10, 100, 20), "Bateria:" + chargeDisp ); //Display for charge. 

}

Could you guys help me?

Comment
Add comment
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

1 Reply

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

Answer by robertbu · Jan 11, 2013 at 10:36 AM

There are several ways of making an indicator. Without knowing more about your goal, it's hard to give specifics. Here are a few:

Have a small green plane on top of a red plane. Move and/or size the green plane based on the charge.

Have a series of game objects each representing a level of charge. Based on your charge, set each one to on/off/red/green/light/dark. This can be done by either swapping the textures or with some shaders, simply setting a the renderer.material.color property.

Create 11 textures...one for every 10% from 0 to 100. Swap the textures based on the charge level.

Set the pixels of the texture for the indicator directly.

Comment
Add comment · 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

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

10 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

Related Questions

Recharging battery script, help 1 Answer

Flashlight battery script 3 Answers

Battery Pickup Regeneration 2 Answers

How to make Battery Bar? 2 Answers

-light.intensity, light.spotAngle and light.color with variable? 0 Answers


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