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 casperskyly · Mar 05, 2013 at 07:02 PM · mouselightscrollintensityscrollwheel

Light intensity with mouse scrollwheel

I have a flashlight script and I want to make the intensity of the light higher and lower with scrolling on 3 levels. The 3 levels are the lowLight, normalLight and highLight. That is so I can adjust the drainage. How to do that? Here's the script, it might help others too.

 var lightSource : Light;
 static var energy : float = 100;
 private var turnedOn : boolean = false;
 var drainSpeedNormalLight : float = 2.0;
 var drainSpeedLowLight : float = 2.0;
 var drainSpeedHighLight : float = 2.0;
 static var hasFlashlight : boolean = false;
 var highLight : float = 2.5;
 var normalLight : float = 1.2;
 var lowLight : float = 0.75;
 
 function Update () {
     if (Input.GetKeyDown(KeyCode.F)&& hasFlashlight|| Input.GetMouseButtonDown(1)&& hasFlashlight)
         ToggleFlashlight();
     if (Input.GetKeyDown(KeyCode.F)&& hasFlashlight|| Input.GetMouseButtonDown(1)&& hasFlashlight)
         audio.Play();
 }
     
 function ToggleFlashlight () {
     turnedOn=!turnedOn;
     if (turnedOn && energy>0) {
        TurnOnAndDrainEnergy();
     } else {
        lightSource.enabled = false;
        turnedOn = false;
     }
 }
 
 function TurnOnAndDrainEnergy () {
     lightSource.enabled = true;
     if (Input.GetAxis("Mouse ScrollWheel") && lowLight)
         lightSource.intensity += normalLight;
     if (Input.GetAxis("Mouse ScrollWheel") && highLight)
         lightSource.intensity -= normalLight;
     if (Input.GetAxis("Mouse ScrollWheel") && normalLight)
         lightSource.intensity += highLight;
         lightSource.intensity -= lowLight;
     while (turnedOn && normalLight){
        energy -= drainSpeedNormalLight*Time.deltaTime;
        yield;
        }
     while (turnedOn && highLight){
        energy -= drainSpeedHighLight*Time.deltaTime;
        yield;
        }
     while (turnedOn && lowLight){
        energy -= drainSpeedLowLight*Time.deltaTime;
        yield;
        }
     lightSource.enabled = false;
     turnedOn = false;
 }
 
 static function AlterEnergy (amount : int) {
     energy = Mathf.Clamp(energy+amount, 0, 100);
 }
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 robertbu · Mar 06, 2013 at 08:54 AM

Here is the basis for making a change (untested). This is more than three levels (i.e. the mouse wheel with run the intensity up and down without restriction right now). You'll likely want to put in some bounds checks. Integrate it into Update(). You will need to declare and assign values for 'intensityFactor' and 'drainFactor';

 if (turnedOn) {
      float f = Input.GetAxis("Mouse ScrollWheel");
      lightSource.intensity += f * intensityFactor;
      drainSpeed += f * drainFactor;
 }
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

How to use Trackpoint middle mouse button for scrolling, 1 Answer

RTS Camera - Zoom is not uniform, yet code is? 2 Answers

How to prevent Overlapping Lights from combining / adding intensity? 3 Answers

Is it possible to distinguish between mouse and trackpad scroll? 3 Answers

GUI Problem 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