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 LEMONVirus99 · Mar 13, 2013 at 07:53 PM · javascriptjava

Have a 'Torch' script toggle on and off

So far i have searched and i have found a script which is perfect for what i am doing although there is no toggle on and off. the script is a timer which after a certain amount of time, it flickers the torch then 'R' is pressed to reset it. I would like to have 'F' as a toggle on and off key, could anyone help me out telling me where that code would go?

Thanks to @alucardj who provided me with this script in his 'slender like game tutorial'.

 private var startIntensity : float = 1.85;
 
 enum flashlightState { IsWorking, Flickering, Resetting }
 
 var currentState : flashlightState;
 
 private var workingTimer : float = 0.0;
 private var workingTimeLimit : float = 90.0;
 
 var minFlickerSpeed : float = 0.05;
 var maxFlickerSpeed : float = 0.2;
 
 private var flickerCounter : float = 0.0;
 
 private var resetTimer : float = 0.0;
 
 function Start() 
 {
     workingTimeLimit = Random.Range( minWorkingTime, maxWorkingTime );
 
     light.enabled = true;
     startIntensity = light.intensity;
 
     currentState = flashlightState.IsWorking;
 }
 
 function Update() 
 {
     switch( currentState )
     {
         case flashlightState.IsWorking :
            IsWorking();
         break;
 
         case flashlightState.Flickering :
            FlickerFlashlight();
            CheckForInput();
         break;
 
         case flashlightState.Resetting :
            Resetting();
         break;
     }
 }
 
 function IsWorking() 
 {
     workingTimer += Time.deltaTime;
 
     if ( workingTimer > workingTimeLimit )
     {
        flickerCounter = Time.time + Random.Range( minFlickerSpeed, maxFlickerSpeed );
 
         currentState = flashlightState.Flickering;
     }
 }
 
 function FlickerFlashlight()
 {
     if ( flickerCounter < Time.time )
     {
         if (light.enabled)
         {
             light.enabled = false;
         }
         else
         {
             light.enabled = true;
 
             light.intensity = Random.Range(minLightIntensity, maxLightIntensity);
         }
 
         flickerCounter = Time.time + Random.Range( minFlickerSpeed, maxFlickerSpeed );
     }
 }
 
 function CheckForInput()
 {
     if (Input.GetKeyDown(KeyCode.R))
     {
         currentState = flashlightState.Resetting;
     }
 }
 
 function Resetting() 
 {
     resetTimer += Time.deltaTime;
 
     if ( resetTimer > 0.75 )
     {
        resetTimer = 0.0;
        workingTimer = 0.0;
        workingTimeLimit = Random.Range( minWorkingTime, maxWorkingTime );
        light.enabled = true;
         light.intensity = startIntensity;
         currentState = flashlightState.IsWorking;
     }
     else if ( resetTimer > 0.65 )
     {
        light.enabled = false;
     }
     else if ( resetTimer > 0.55 )
     {
        light.enabled = true;
         light.intensity = startIntensity;
     }
     else if ( resetTimer > 0.25 )
     {
        light.enabled = false;
     }
     else if ( resetTimer > 0.15 )
     {
        light.enabled = true;
         light.intensity = startIntensity;
     }
     else if ( resetTimer > 0.05 )
     {
        light.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
2
Best Answer

Answer by DanFrias · Mar 13, 2013 at 10:48 PM

I would modify the code to allow the CheckForInput function to be performed every Update cycle, allowing it to capture the F input key. When the F input key is detected it will toggle the light.enabled value. This would then require the logic of the switch statement be placed into the CheckForInput function in the instance when the R key is pressed. The required code changers are below.

In the Update function, simply move the CheckForInput function out of the switch, like this:

 function Update() 
 {
     switch( currentState )
     {
         case flashlightState.IsWorking :
            IsWorking();
         break;
  
         case flashlightState.Flickering :
            FlickerFlashlight();
         break;
  
         case flashlightState.Resetting :
            Resetting();
         break;
     }

     CheckForInput();
 }

Then, in the CheckForInput function, add the logic from the Update function for the R key press and add the logic for the F key press.

Update: I had originally omitted a close parenthesis below. Thank to alucardj for pointing this out.

 function CheckForInput()
 {
     if (Input.GetKeyDown(KeyCode.R) && currentState == flashlightState.Flickering)
     {
         currentState = flashlightState.Resetting;
     }
 
     if (Input.GetKeyDown(KeyCode.F) )
     {
         light.enabled = !light.enabled;
     }
 }
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 AlucardJay · Mar 14, 2013 at 02:54 AM 1
Share

@LE$$anonymous$$ONVirus99 : it would have been nice if you gave me credit, or even provide the link to where you found my script for future readers.

Answer is excellent, upvoted.

avatar image LEMONVirus99 · Mar 14, 2013 at 04:08 PM 0
Share

$$anonymous$$y apologies @alucardj I came across this post some time ago and since I've been making the levels i completely forgot to retrace the source of this script. The script should work however, i keep on getting an error message. I dont really know java as of now so could you tell me how to work around this? i seem to get this error a lot Thanks again

alt text

screen shot 2013-03-14 at 16.26.23.png (21.7 kB)
avatar image AlucardJay · Mar 14, 2013 at 04:56 PM 1
Share

yeah, sorry, but it happens all the time. You were just unfortunate enough to get my comment before my first coffee this morning, when I saw it.

With your problem, you have not closed the parentheses in your conditional. Translation : you forgot a )

 if ( Input.Get$$anonymous$$eyDown( $$anonymous$$eyCode.F ) )
 {
     light.enabled = !light.enabled;
 }

the conditional

 if (  )

the argument

 Input.Get$$anonymous$$eyDown( $$anonymous$$eyCode.F )


DanFrias you forgot a parentheses! But you still get my upvote =]

avatar image LEMONVirus99 · Mar 14, 2013 at 05:05 PM 0
Share

Now i know :) Works brilliantly. Thank you again for your help!

avatar image DanFrias · Mar 14, 2013 at 06:09 PM 0
Share

So I did forget the close parenthesis :). Thanks for correcting that alucardj

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

12 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

Related Questions

Unity,How to make training Target Enemy 1 Answer

[SOLVED] Java Converters In Unity? 2 Answers

Convert this line of javascript to C# (easy) 1 Answer

Creating a file and writing into it? :p 1 Answer

oescape script name? 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