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 StickyDream · Aug 19, 2013 at 10:32 AM · lightflashlightflickering

Flashlight Flickering

Hello Unity members id like to make my flashlight (that runs on batteries) flickering when the battery is low heres my script(its made in c#):

using UnityEngine; using System.Collections;

public class Flashlight : MonoBehaviour { public static float BatteryLife = 100; public static GameObject HeadLightMount;

 public Light HeadLight;
 public float BatteryReductionSpeed = 3.0f;
 
 public AudioClip on;
 public AudioClip off;
 
 private AudioSource audiosource;
 
 void Awake()
 {
     audiosource = GameObject.Find("Main Camera").GetComponent<AudioSource>();
     
     HeadLightMount = GameObject.FindWithTag("Headlamp");
     HeadLight.enabled = false;
 }
 
 void Update()
 {
     if(HeadLight.enabled)
     {
         BatteryLife = BatteryLife - (BatteryReductionSpeed * Time.deltaTime);
     }
     
     if(Input.GetKeyDown(KeyCode.F) && HUD.HasFlashlight && !HeadLight.enabled)
     {
         audiosource.clip = on;
         audiosource.Play();
         
         if(BatteryLife <= 0 && HUD.BatteryCount > 0)
         {
             HUD.BatteryCount--;
             BatteryLife = 100;
         }
         
         HeadLight.enabled = true;
     }
     else if(Input.GetKeyDown(KeyCode.F) && HUD.HasFlashlight && HeadLight.enabled)
     {
         audiosource.clip = off;
         audiosource.Play();
         
         HeadLight.enabled = false;
     }
     
     if(BatteryLife <= 0)
     {
         BatteryLife = 0;
         HeadLight.enabled = false;
     }
 }

}

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
0

Answer by sfc.itzhak · Aug 19, 2013 at 11:09 AM

hey mate,

you have 2 options:

1.writing a script or a function that turns on and off the flashlight to simulate flickering.

2.do an animation of this flickering and playing it.

i prefer the animation cause it gives you more freadom and can look more real and dynamic then doing it in a script,

script sample:

add this function and call it by " StartCoroutine(FlashLightFlicker());"

     IEnumerator FlashLightFlicker()
     {
 
         while (true)
         {
             HeadLight.enabled = !HeadLight.enabled;
 
 
             yield return new WaitForSeconds(Random.Range(0,1));
         }
 
 
     }

and see the animation in the attached package.

link

sfc

Comment
Add comment · Show 3 · 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 StickyDream · Aug 19, 2013 at 11:12 AM 1
Share

I dont understand what u mean by that script im kind of a noob, cant you put it in too my script ? :/

avatar image sfc.itzhak · Aug 19, 2013 at 12:08 PM 0
Share

you have a link just download and import and see for yourself

sfc.

avatar image ArkaneX · Aug 19, 2013 at 12:19 PM 0
Share

@StickyDream - I understand that you're trying to write a game and get some help here, but without proper knowledge, you will not be able to complete what you wish.

I strongly suggest reading through Unity documentation, watching the tutorials and checking pages like Unity Gems. You will spend some time on this, true, but then you'll understand other people answers.

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

17 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 avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Flashlight Flicker! 2 Answers

Flashlight turning off, but not back on again? 4 Answers

Flashlight won't turn off. 2 Answers

Flashlight effect 2 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