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 xhybridxreflex · Apr 13, 2013 at 03:35 PM · flashlightintensityscrollwheel

Flashlight Intensity with ScrollWheel

I'm trying to make my flashlight change its intensity by using the scrollwheel up and down. I figured it'd be a drain light script. I'm not so good at scripting though, so I gave it a shot anyways and this is what I came up with.

 function Update ()
 
 if(turnedOn) {
      float f = Input.GetAxis("Mouse ScrollWheel");
      lightSource.intensity += f * intensityFactor;
      drainSpeed += f * drainFactor;
 }

If anybody could help me i'd be forever greatful

Thanks

Comment
Add comment · Show 3
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 Unitraxx · Apr 13, 2013 at 05:37 PM 1
Share

And what exactly isn't working?

avatar image xhybridxreflex · Apr 20, 2013 at 08:20 AM 0
Share

Its not working...?

avatar image dendens2 · Apr 20, 2013 at 02:29 PM 0
Share

What part of the script is not working though? Is it not draining? Is the intensity not going up or down?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Raydaq · Apr 20, 2013 at 03:44 PM

I believe that Input.GetAxis("Mouse ScrollWheel"); returns the delta (or change in) the axis from the last update.

Therefore you need to calculate where you are in your range using that change. Mathf.Clamp: will let you set a minimum and maximum range

     var maxIntensity: float = 10; //max
     var minIntensity: float = 1; //min
     var changeSpeed: int = 5;
 
     
     function Update(){
         lightSource.intensity = Mathf.Clamp(lightSource.intensity - Input.GetAxis("Mouse ScrollWheel")*changeSpeed, minIntensity, maxIntensity);
     }


So say our intensity is currently 5, and we click the scroll wheel down once (lets say this = 0.2).

Then script says:

New intensity = 5 - 0.2 * 5

New intensity = 5 - 1

New intensity = 4

(Also mathf.Clamp will make sure that this is within the min an max range.)

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
avatar image
0
Wiki

Answer by Jazzer008 · Apr 20, 2013 at 03:38 PM

The change in intensity should be working. Be mindful that most mouse wheel inputs give small numbers such as 0.1. You may need to increase your intensity factor to notice a difference. I would have the intensity factor at around 10.0 .

Secondly you haven't shown any other code surrounding your drainSpeed variable. The drainSpeed variable will increase in proportion with an increase in your light intensity. You should have some code else where that will minus the drainSpeed variable from a 'battery' variable. I would put this battery variable at 100.0 . Once the battery reaches 0.0 you should change turnedOn to false.

You will also obviously need a chargeSpeed variable, that only acts upon the battery when turnedOn is false. I'll just put the code below. :P

 var lightSource : GameObject;
 var totalCharge : float = 100.0;
 var chargeSpeed : float = 5.0;
 var intensityFactor : float = 10.0;
 var drainFactor : float = 1.0;

 private var turnedOn : boolean = false;
 private var currentCharge : float;
 private var drainSpeed : float;

 function Start (){
    currentCharge = totalCharge;
 }
 
 function Update (){
  
    if(turnedOn && currentCharge > 0.0) {
       var wheelInput : float = Input.GetAxis("Mouse ScrollWheel");
       lightSource.intensity += wheelInput * intensityFactor;
       drainSpeed += wheelInput * drainFactor;
       currentCharge -= drainSpeed;
 
    }else if(currentCharge <= totalCharge){
       turnedOn = false;
       lightSource.intensity = 0.0;
       currentCharge += chargeSpeed;
    }
 
   if(Input.GetKeyDown("f")){
     if(turnedOn){
        turnedOn = false;
     }else{
        turnedOn = true;
     }
   }
 
 }
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

13 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

Related Questions

Flashlight timer 2 Answers

Light intensity with mouse scrollwheel 1 Answer

Webgl build disables mouse wheel scroll on webpage. 4 Answers

Increase Light Intensity in Coroutine 1 Answer

FlashLight Turning off, but not back on again? (Simple Script Fix?) 4 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