• 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

3 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

141 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 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 accurately map UV from second texture in image effects 0 Answers

Beginner Here: Want some help coding my player (a cube) to move along a plane I made. (code inside) 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges