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 /
avatar image
0
Question by OBIC_Muse · Jul 27, 2019 at 05:09 PM · lightingtimetime.deltatimeincreasemathf.lerp

Mathf.lerp to increase intensity and range of pointlight

Hello hello dear people it's Anna! I am working on a piece where I have point lights with rigid bodies flying around. When they collide with certain game Objects they change color and make a sound. This already works perfectly. But now I want them to increase range and intensity over time and then go back to normal (maybe during 2 sec). For some reason it doesn't seem to be working. This is the code at the moment:

public float minimumIntensity = 7f; public float maximumIntensity = 100f; public float minimumRange = 1f; public float maximumRange = 10f;

 //starting value for lerp
 //Make slider
 [SerializeField]
 [Range(0f, 1f)]
 public float t = 0.0f;

 void OnCollisionEnter(Collision collisionInfo)
 {
     if (collisionInfo.collider.tag == "Musicplant" || collisionInfo.collider.tag == "Plant")
     {
         currentColor = colorList[random.Next(0, colorList.Count - 1)];

         pointLight.intensity = Mathf.Lerp(minimumIntensity, maximumIntensity, t);
         pointLight.range = Mathf.Lerp(minimumRange, maximumRange, t);

         //increase the t interpolater
         t += 0.2f * Time.deltaTime;


Once this works, I would add this second part to decrease it again:

         // now check if the interpolator has reached 1.0
         // and swap maximum and minimum so game object moves
         // in the opposite direction.
         if (t > 1.0f)
         {
             float temp = maximumIntensity;
             maximumIntensity = minimumIntensity;
             minimumIntensity = maximumIntensity;
             minimumIntensity = temp;

             float tempp = maximumRange;
             maximumRange = minimumRange;
             minimumRange = maximumRange;
             minimumRange = tempp;

             t = 0.0f;
         }
     }

 }  

I think the problem lies with time.deltaTime since pointLight.intensity += 20f works but pointLight.intensity += 20f * time.deltaTime doesn't do anything at all.

Anybody has an idea or a solution? Thanks for reading!

Comment
Add comment · Show 6
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 Dragate · Jul 27, 2019 at 06:07 PM 0
Share

The thing is that OnCollisionEnter() gets called only once when colliding with something and thus, you barely see any effect. $$anonymous$$aybe you should start a coroutine once the collision starts. Also, do take a look at $$anonymous$$athf.PingPong() since you want to reverse the effect afterwards.

avatar image OBIC_Muse · Jul 29, 2019 at 10:37 AM 0
Share

Oooh I see! Thank you so much for the quick answer @Dragate

avatar image xxmariofer OBIC_Muse · Jul 29, 2019 at 11:17 AM 0
Share

OnCollisionEnter is a coroutine, so you can treat it as one, just changing void for IEnumerator and adding the yield instruction

avatar image OBIC_Muse xxmariofer · Jul 30, 2019 at 01:11 PM 0
Share

Heey guys, So I put it in a coroutine and start it onCollision Enter. To test it I'm still simply using pointLight.intensity += 5f * Time.deltaTime; And it's still not working... without Time.deltaTime it is working though. What am I missing here?

void OnCollisionEnter(Collision collisionInfo) { if (collisionInfo.collider.tag == "$$anonymous$$usicplant" || collisionInfo.collider.tag == "Plant") { currentColor = colorList[random.Next(0, colorList.Count - 1)];

         StartCoroutine(flashLight());

      
     }
 }

     public float $$anonymous$$imumIntensity = 7f;
     public float maximumIntensity = 100f;
     public float $$anonymous$$imumRange = 1f;
     public float maximumRange = 10f;

     //starting value for lerp
     [SerializeField]
     [Range(0f, 1f)]
     public float t = 0.0f;

 IEnumerator flashLight()
 {
     pointLight.intensity += 5f * Time.deltaTime;
     pointLight.range += 1f * Time.deltaTime;

     /*
     pointLight.intensity = $$anonymous$$athf.Lerp($$anonymous$$imumIntensity, maximumIntensity, t);
        pointLight.range = $$anonymous$$athf.Lerp($$anonymous$$imumRange, maximumRange, t);

    //increase the t interpolater
    t += 0.2f * Time.deltaTime;

    // now check if the interpolator has reached 1.0
    // and swap maximum and $$anonymous$$imum so game object moves
    // in the opposite direction.
    if (t > 1.0f)
    {
        float temp = maximumIntensity;
        maximumIntensity = $$anonymous$$imumIntensity;
        $$anonymous$$imumIntensity = maximumIntensity;
        $$anonymous$$imumIntensity = temp;

        float tempp = maximumRange;
        maximumRange = $$anonymous$$imumRange;
        $$anonymous$$imumRange = maximumRange;
        $$anonymous$$imumRange = tempp;

        t = 0.0f;
    }*/

     yield return new WaitForSeconds(10);
 }
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by mjahlv · Jul 30, 2019 at 06:30 PM

The problem is that OnCollisionEnter only runs when a collision is detected, and so your code only runs when it detects a collision.

Time.deltaTime is the time since the last frame. If you do 20f * time.Delta time your result will be a single value that you then add on. This approach is correct, however you're only doing it once when you register a collision when instead you need to do it every frame for 2 seconds.

What you want is a coroutine that executes on collision:

 //We want somewhere to store the coroutine so we can stop it on another collision
     Coroutine myCoroutine; 
 
     void OnCollisionEnter(Collision collisionInfo)
     {
         if (collisionInfo.collider.tag == "Musicplant" || collisionInfo.collider.tag == "Plant")
         {
             //Set the colour of the light on collision
             currentColor = colorList[random.Next(0, colorList.Count - 1)];
 
             //First we want to check for an existing coroutine. If we have one, we need to stop it!
             if (myCoroutine != null)
                 StopCoroutine(myCoroutine);
 
             //Then we initiate a new coroutine to run the light
             myCoroutine = StartCoroutine(LightCoroutine());
         }
     }
 
     IEnumerator LightCoroutine()
     {
         float t = 0;
 
         while (t < 1f) //This will take 1 second
         {
            //T will work towards 1, giving us the maximum intensity
             pointLight.intensity = Mathf.Lerp(minimumIntensity, maximumIntensity, t); 
             pointLight.range = Mathf.Lerp(minimumRange, maximumRange, t);
             t += Time.deltaTime; //Add the time since the last frame
             yield return null; //Wait for the next frame
         }
 
         while (t > 0f) //This will take 1 second
         {
             pointLight.intensity = Mathf.Lerp(minimumIntensity, maximumIntensity, t);
             pointLight.range = Mathf.Lerp(minimumRange, maximumRange, t);
             t -= Time.deltaTime; //Subtract the time since the last frame
             yield return null; //Wait for the next frame
         }
     }

The coroutine is then called every time there is a valid collision. It will stop the old one beforehand too in case you get another collision within 2 seconds. What would happen is the coroutine will run again starting from 0!

There's probably other approaches towards this that might be better, but I thought I would try to retain the basic logic of your code in my answer!

More info on coroutines: https://docs.unity3d.com/Manual/Coroutines.html

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 OBIC_Muse · Aug 06, 2019 at 07:10 PM 0
Share

This works and I understand now what I did wrong. Thank you soo much! I'm super happy to see the glows everywhere now :D

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

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

Related Questions

Help with my "speeding up time" script 1 Answer

How can I make a GUI element move independently of the frame rate? 1 Answer

Unity Simple Clock Tutorial with custom time? 1 Answer

How to make this display milliseconds? 3 Answers

RPG Action Progress Bar always same speed 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