Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by Cygnus138b · Feb 12, 2016 at 07:58 PM · lightingsmoothtransitionsmathf.lerp

How to smoothly change a value up and down based on key press?

Hi,

I was wondering if anyone could help me out here. Basically, I have a point light, and I want it to slowly increase in intensity while a button is held down, but then slowly decrease back to 0 once the button is released. I've fiddled around with the Mathf.Lerp function in C#, but I can't seem to get it to work. Anyone have any ideas? Thank you so much to anyone who takes the time to answer. :)

EDIT: I got it to work, but I still have a problem. It smoothly increases while the key is held, and smoothly decreases once it's released, but my problem is that there is a delay of a few seconds between when the key is held down and when the light begins to increase in intensity. Any ideas? Here's my current code.

 using UnityEngine;
 using System.Collections;
 
 public class LightController : MonoBehaviour {
 
     public Light thrustLight;
 
     public float tParam = 0;
     public float lightSpeed = 5.0f;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
         if (Input.GetKey ("up")) {
 
             if (tParam < 1) {
 
                 tParam += Time.deltaTime * lightSpeed;
 
                 thrustLight.GetComponent<Light> ().intensity = Mathf.Lerp (0.1f, 1.8f, tParam);
 
             }
         } 
 
         else {
 
                 tParam -= Time.deltaTime * lightSpeed;
 
                 thrustLight.GetComponent<Light> ().intensity = Mathf.Lerp (0.1f, 1.8f, tParam);
 
         }



EDIT AGAIN: I solved it! Because I didn't have an " if (tParam > 0) { }" in my else, the value was going backwards with time, making it delay. Putting that in there makes sure it doesn't go backwards and stays within 0 to 1. Here's my working code for anyone currently trying to google solve this issue:

 using UnityEngine;
 using System.Collections;
 
 public class LightController : MonoBehaviour {
 
     public int thrustForce = 5;
     public Light thrustLight;
 
     public float tParam = 0;
     public float lightSpeed = 5.0f;
 
     void Update () {
     
         if (Input.GetKey ("up")) {
 
             if (tParam < 1) {
 
                 tParam += Time.deltaTime * lightSpeed;
 
                 thrustLight.GetComponent<Light> ().intensity = Mathf.Lerp (0, 1.8f, tParam);
 
             }
         } 
 
         else {
 
             if (tParam > 0){
 
                     tParam -= Time.deltaTime * lightSpeed;
 
                 thrustLight.GetComponent<Light> ().intensity = Mathf.Lerp (0, 1.8f, tParam);
 
             }
         }
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

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

How to fix lighting on spheres made in Blender? 1 Answer

Lighting bakes non-static objects 1 Answer

hi in the scen wive lighting changes not visible and light properety shows me it how fix it ? 0 Answers

2D shader / lighting like Terraria or Starbound 2 Answers

how can I set Lightmap Scale per object 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