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 Red Sentinel · Jan 17, 2013 at 11:48 PM · lightspotlightflashlightbattery

Flashlight battery script

Hi everyone. I am trying to write a script that will make a spotlight/flashlight run for a certain amount of time, flicker and make a buzzing sound after say 10 minutes and dim slightly, then turn off completely after another 5 minutes. I also tried to make a script so the player could pick up batteries which would replenish the flashlight. So far total failure. I am new to scripting so scripting in JavaScript would be great. Could I have some help with this, please? Thanks for any input.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by jmgek · Jan 17, 2013 at 11:59 PM

 public float FlashlightTime = 1000f; //change the 1000 to add or subtract time. 
 
     private void Start()
         {
             timeStarted = Time.time;      
         }
         private void Update()
         {
         
             if (Time.time > timeStarted+ FlashlightTime)
             {
                //do stuff
         }
     }

This is in c# remeber to mark as answered.

Comment
Add comment · Show 1 · 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 Red Sentinel · Jan 18, 2013 at 12:03 AM 0
Share

Thanks, I'll get back to you when I'm on my laptop.

avatar image
0

Answer by robertbu · Jan 18, 2013 at 05:07 AM

Here is a starter flashlight battery script. It uses an Animation curve, so you will need to author how you want the flashlight to behave over time. Attach it to a light and click on the public ac property. You can zoom in and change the characteristics of nodes.

 using UnityEngine;
 using System.Collections;
 
 public class Flashlight : MonoBehaviour
 {
     public float fRunTimeFullCharge = 900.0f;
     public AnimationCurve ac = new AnimationCurve();
 
     float fTimer = 0.0f;
     float fMaxIntensity;
     public float chargeLevel; // Can't set level here
     
     public void SetChargeLevel(float fCharge)
     {
         fTimer = (1.0f - Mathf.Clamp01(fCharge)) * fRunTimeFullCharge; 
         Debug.Log (fTimer);
     }
     
     void Start ()
     {
         Light light = GetComponent<Light>();
         fMaxIntensity = light.intensity;
         chargeLevel = 1.0f;
     }
     
     void Update ()
     {
         if (fTimer < 0.0f || fTimer > fRunTimeFullCharge)
             return;
         chargeLevel = fTimer/fRunTimeFullCharge;
         light.intensity = ac.Evaluate (chargeLevel);
         fTimer += Time.deltaTime;
     }

}

And here is the animation curve I used for testing. The flashing is pulled out in the circle.

alt text


curve.jpg (143.3 kB)
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 Red Sentinel · Jan 18, 2013 at 05:16 PM 0
Share

Great that's fantastic I'll test this out once I'm back on my laptop.

avatar image Mayday556 · Aug 02, 2013 at 03:02 AM 0
Share

Wow this is really helpful

avatar image
0

Answer by MasonicGryphon7 · Jan 18, 2013 at 06:25 PM

This is in JavaScript so you know :D

Added a display for the battery also...

 //Name this script Flashlight and attach it to your player for instance
 
 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") + "%");
 }
Comment
Add comment · Show 1 · 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 DinoBoy300 · Oct 19, 2015 at 03:50 AM 0
Share

@$$anonymous$$asonicGryphon7 Do I just have to add this to the character/player, because if so, this sint working for me.

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

How do I make a power out on my flashlight? 1 Answer

Make spotlight flicker when seeing enemy? 1 Answer

Spotlight Piercing through game objects? 0 Answers

Flashlight Battery Error No definition 1 Answer

-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