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 Rnst1 · Aug 17, 2012 at 06:37 PM · javascripttime.deltatimevector3.lerpcolor.lerp

Can't figure out how Time.deltaTime can works in this case

Hello,

I must say that I begin in Unity scripting. ( I did the Walker Boys tutorials which introduced me well ).

Here is the situation : When using Time.deltaTime with a Vector3.Lerp, it acts in a way and when using with Color.Lerp, it acts another way;

01- With Vector3.Lerp

 var keyUpEnabled        : boolean        = true;
 var keyUpPos            : Transform;
 var keyUpSpeed             : float            = 8.0;            // speed for the key when getting up

// position for the key when up

 function Update () 
 {
 KeyUpAnim ()
 }
 
 
 function KeyUpAnim ()    // up animation
 {
          if ( keyUpEnabled )
          {
              transform.position = Vector3.Lerp ( transform.position, keyUpPos.transform.position, keyUpSpeed * Time.deltaTime );        // up animation
              if ( transform.position == keyUpPos.transform.position )
             {
                 keyUpEnabled = false;
             }
          }
 }

When looking at the doc for Vector3.Lerp, that says : "static function Lerp (from : Vector3, to : Vector3, t : float) : Vector3 " "t is clamped between [0...1]. When t = 0 returns from. When t = 1 returns to. When t = 0.5 returns the average of from and to. "

I checked Time.deltatime by printing it every frame and it stays around 0.015, so in this case t = 8 X 0.015 so 0.12. Since t is relatively constant and doesn't reach 1, the game object with this script should stay near the start position. But I can't figure out why : it makes a smooth movement from the start position to the keyUpPos position as if t was gently changing from 0 to 1.

02- With Color.Lerp

 // Colors

 var yellow                : Color                = Vector4 (1, 0.92, 0.016, 1);

 var red                    : Color                = Vector4 (1, 0, 0, 1);
 
 
 // Toggles
 var colorLerpEnabled    : boolean            = true;
 
 function Update () 
 {
     ColorLerp ();
     
 }
 
 function ColorLerp ()
 {
     if ( colorLerpEnabled )
     {
         
         renderer.material.color = Color.Lerp ( yellow, red, 8*Time.time );
         //yield WaitForSeconds ( 4) ;
         if ( renderer.material.color == red )
         {
             colorLerpEnabled = false;
         }
     }
 }

Contrary to the precedent exemple, in that case, the game object with this script attached got an oranged yellow color due to the constant value of t = 0.12. ( so according to plan ).

That makes no sens to me in the first exemple.

Thanks if you got any idea.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Aug 17, 2012 at 07:33 PM

That's why the first example looks totally different (and i don't talk about that it uses Vector3 instead of Color). In the first example you use the current value as lerp start value and the second value is your "target" value. This is actually a trick where you can use lerp to get a smooth decelerated movement / fading.

The usual way how lerp is used can be seen in the answer of Khada.

If you want this "decelerated" fading, use it like in the first example:

 renderer.material.color = Color.Lerp ( renderer.material.color, red, Time.deltaTime );

Note that you can't compare renderer.material.color to red because you will never reach the full color red with this method.

For example. When this line is executed C will be 90% of it's old value and 10% red. When it's executed again it will again take 90% of the old value, which is already a little bit red, and 10% red.

 C = Color.Lerp (C, red, 0.1 );

It slowly get's closer to red, but it will never be red.

Lerp isn't doing something magic. It really just do this:

 function Lerp(Start : Vector3, End : Vector3, t : float) : Vector3
 {
     t = Mathf.Clamp01(t); // keep t within 0 and 1
     return Start*(1-t) + End*t;
 }
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 Rnst1 · Aug 19, 2012 at 11:40 AM 0
Share

Thank you for taking time to explain me! :) I didn't know about "the trick" used when using current value. Thank you again.

avatar image
2

Answer by Khada · Aug 17, 2012 at 07:17 PM

Lerp your Vector3 like this:

 Vector3 start;
 Vector3 end;
 float t = 0; //<- starts at 0
 
 void Update()
 {
    Vector3 newPos = Vector3.Lerp(start, end, t);
    t += Time.deltaTime;
 }
Comment
Add comment · Show 2 · 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 Rnst1 · Aug 19, 2012 at 11:42 AM 0
Share

Sorry for my late reply. Thanks for the hint !

avatar image Khada · Aug 19, 2012 at 11:49 AM 0
Share

No worries :)

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

Trying to set up a lerp position system for the camera and failing... 2 Answers

Stealth AI Script. If less than or equal to not working correctly. Any fixes ? 1 Answer

Setting Scroll View Width GUILayout 1 Answer

Unable to Color.Lerp in two directions (Solved) 1 Answer

Why is the "t" value clamped to [0, 1] in Lerp functions? 3 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