Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 tuomvii · Mar 02, 2018 at 02:12 PM · scripting problemvaluesmathf.lerp

Mathf.Lerp doesn't work on light object's value change

I am trying to make a smooth transition between changing a light's range value when a lid is on or off a flame. It doesn't really work right now cause it jumps straight to 8 from 2. Well it's really something like 2.121 -> 7.98 but you get the point. What I'm doing wrong?

 public Animator anim;
 public bool lidCheck;
 public Light myLight;
 public float minimum = 2.0f;
 public float maximum = 8.0f;
 public float smooth = 2.0f;

 void Start()
 {
     anim = GetComponent<Animator>();
     lidCheck = false;
 }

 void Update()
 {
     if (lidCheck == false)
     {
         myLight.range = Mathf.Lerp(minimum, maximum, Time.deltaTime * smooth);
     }
     if (lidCheck == true)
     {
         myLight.range = Mathf.Lerp(maximum, minimum, Time.deltaTime * smooth);
     }
 }

 void OnTriggerStay (Collider other)
 {
     if (other.gameObject.tag == "Player")
     {
         if (Input.GetKey(KeyCode.E))
         {
             anim.Play("openlid");
             lidCheck = true;
         }

         if (Input.GetKey(KeyCode.R))
         {
             anim.Play("closelid");
             lidCheck = 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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Helical · Mar 02, 2018 at 02:35 PM

Try

 myLight.range = Mathf.Lerp(myLight.range , maximum, Time.deltaTime * smooth);

Lerp as a function goes from Point A to Point B by a value of 0 to 1.

In you line, inside the Update loop

 myLight.range = Mathf.Lerp(minimum, maximum, Time.deltaTime * smooth);

suppose minimum = 0.2; maximum = 0.8;

and Time.deltaTime * smooth = 0.5; //i know its much less

Every single update your range will become

range = minimum + (maximum - minimum) * 0.5f;

And the next update you will start all over again, never reaching the target

Usually in lerps you want to feed the Point A the actual value that is chaning

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 tuomvii · Mar 02, 2018 at 02:41 PM 0
Share

Now the Light range doesn't change at all when I tried your method. Hmm... weird.

avatar image
0

Answer by yummy81 · Mar 02, 2018 at 04:32 PM

I rewrote your code a bit. First, I changed your Update loop entirely. Next, I moved your code from OnTriggerStay to OnTriggerEnter with coroutine. The reason is that you should check for inputs (GetKey) in Update loop, not in FixedUpdate. OnTriggerStay acts as FixedUpdate, while OnTriggerEnter with coroutine (with while loop yielding null) works as Update. Here's the script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class tuomvii : MonoBehaviour
 {
     public Animator anim;
     public bool lidCheck;
     public Light myLight;
     public float minimum = 2.0f;
     public float maximum = 8.0f;
     public float smooth = 2.0f;
     
     private float t;
     private Coroutine co = null;
     
     void Start()
     {
         anim = GetComponent<Animator>();
         lidCheck = false;
     }    
     
     void Update()
     {
         t += Time.deltaTime * smooth * (-1 + 2*System.Convert.ToInt32(lidCheck));
         t = Mathf.Clamp01(t);
         myLight.range = Mathf.Lerp(minimum, maximum, t);
     }
     
     void OnTriggerEnter (Collider other)
     {
         if (other.gameObject.CompareTag("Player"))
         {
             co = StartCoroutine(LightCoroutine());
         }
     }
     
     void OnTriggerExit(Collider other)
     {
         if (other.gameObject.CompareTag("Player"))
         {
             StopCoroutine(co);
         }
     }
         
     IEnumerator LightCoroutine()
     {
         while (true)
         {
             if (Input.GetKey(KeyCode.E))
             {
                 anim.Play("openlid");
                 lidCheck = true;
             }
             if (Input.GetKey(KeyCode.R))
             {
                 anim.Play("closelid");
                 lidCheck = false;
             }
             yield return null;
         }
     }
 }
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

Answer by Legend_Bacon · Mar 02, 2018 at 06:59 PM

Hello there,

If you don't want to use arbitrary speeds and values, you could implement something like this:

     private Coroutine cor_turnOnOrOff = null;
     public float f_transitionDuration = 4.0f;
     public Light ref_myLight = null;
 
     private void OnTriggerStay(Collider other)
     {
         if (other.gameObject.tag == "Player")
         {
             if (Input.GetKey(KeyCode.E))
             {
                 TurOnOrOff(true);
             }
             if (Input.GetKey(KeyCode.R))
             {
                 TurOnOrOff(false);
             }
         }
     }
 
     public void TurOnOrOff(bool turnOn)
     {
         if (cor_turnOnOrOff != null)
             StopCoroutine(cor_turnOnOrOff);
 
         cor_turnOnOrOff = StartCoroutine(TurnOnOrOffCoroutine(turnOn));
     }
 
     private IEnumerator TurnOnOrOffCoroutine(bool turnOn)
     {
         float startValue = ref_myLight.range;
         float targetValue = 0.0f;
         float timer = 0.0f;
 
         if (turnOn)
         {
             anim.Play("openlid");
             targetValue = maximum;
         }
         else
         {
             anim.Play("closelid");
             targetValue = minimum;
         }
 
         while (timer < f_transitionDuration)
         {
             timer += Time.deltaTime;
             float t = timer / f_transitionDuration;
             t = t * t * t * (t * (6f * t - 15f) + 10f);
             ref_myLight.range = Mathf.Lerp(startValue, targetValue, t);
             yield return null;
         }
         yield return null;
     }


This might look a little complicated, but it isn't really. All you have to do is call TurnOnOrOff(true); when you want the lamp to turn on, and TurnOnOrOff(false); when you want it off. The transition then occurs over however many seconds you set f_transitionDuration, instead of setting an arbitrary speed.

The line t = t * t * t * (t * (6f * t - 15f) + 10f); is just a smoothing algorithm. If you don't like this one, you can pick another one from this page; keep the one that looks the best for your project!


I hope that helps!

Cheers,

~LegendBacon

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

Answer by KaveeM2007 · Apr 25 at 05:08 AM

Guys, I just found a solution for this. when you assign the result of this (Mathf.Lerp(a,b,t)) you shouldn't do it to a new variable. for example If I want to set "k" to "M" by "t' in every frame using Mathf.Lerp. This is what I'd do - float K = 10; float M = 30;

 float t = 0.2f;
 
 Update()
 {
       Lerp();
 }
 
 void Lerp()
 {
       K = Mathf.Lerp(K ,M, t * Time.deltaTime)`
       float m = K;
       Debug.Log(m);
 }`

What happens is, I set the value returned by Mathf.Lerp to K which is the value I want to interpolate to ITSELF. By this way, in every frame K is updated. If you assign this returned value to a new variable, this K will stay same forever and will give the same value every time you run the code as it is not changing withing the Mathf.Lerp method. this method does not change it's arguments.. But, by assigning the returned value of the method to itself K is UPDATED EVERY FRAME. then you won't stuck in the same value every time you run your code. If you want this K value for another variable, assign it just after the method. (I've done it as a comment) Debug.Log is the way how I took output here.

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

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

Related Questions

Creating an instance of a ScriptableObject, that will not change the base asset. 2 Answers

How to pass slider values through scenes? 2 Answers

Values not moving to Game Over screen or the Game over doesn't somehow work. 0 Answers

How to make a Separate Object follow the Cameras Y Rotation? 1 Answer

Access water4 scripts via my script? 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