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 hatzalex · Dec 08, 2011 at 08:24 AM · movementvector3lerpstop

Stopping Vector3.Lerp

Hallo everybody,

I stumbled against a weird (in my eyes) problem while using Vector3.Lerp. If i place the lerp function inside an if statement which should control when the lerp should actually take place and when the object should stop, it does not behave like i want it to.

 function Update () 
 {    
     calculateDistance();
     
     if (canMove)
     {
         startMoving();
     }
 }
 
 function moveLetters(thisTransform : Transform, startPos : Vector3, endPos : Vector3, time : float)
 {    
     var i = 0.0;
     var rate = 1.0/time;
     
     
     while(i <1.0)
     {    
         i += Time.deltaTime * rate;
         thisTransform.position = Vector3.Lerp(startPos, endPos, i);
         yield;
     }
 
 }
 
 function calculateDistance()
 {    
     if(clonedLetter_1 != null)
     {
        distance_1 = Vector3.Distance(clonedLetter_1.transform.position,player.transform.position);
        distance = distance_1;
        
        if(distance < 5)
        {
                canMove = true;
        } else if (distance > 5)
            {
                canMove = false;
            }
        
        print(distance);
     }
 }
 
 function startMoving()
 {
         
         if(clonedLetter_1 != null)
         {    
             moveLetters(clonedLetter_1.transform,clonedLetter_1.transform.position,target_1.transform.position,30.0);
             clonedLetter_1.transform.position.y = target_1.transform.position.y;
         }
         
         if(clonedLetter_2 != null)
         {
             moveLetters(clonedLetter_2.transform,clonedLetter_2.transform.position,target_2.transform.position,30.0);
             clonedLetter_2.transform.position.y = target_2.transform.position.y;
         }
         
         if(clonedLetter_3 != null)
         {
             moveLetters(clonedLetter_3.transform,clonedLetter_3.transform.position,target_3.transform.position,30.0);
             clonedLetter_3.transform.position.y = target_3.transform.position.y;
         }
 }

The problematic if statement:

  if(distance < 5)
    {
            canMove = true;
    } else if (distance > 5)
        {
            canMove = false;
        }

I printed the results and canMove switches correctly between true and false. Distance also works correctly. The problem is, once the lerp is turned on, it wont stop, even though i have an if statement which should stop it each time the distance is bigger than 5. My question is if lerp is "update" independent (if such a thing exists) or if there is another to stop a Lerp movement.

I am fairly new to scripting so i bid my excuses if this is something obvious which i cant see to unravel.

Anyways, thanks in advance.

Hatzalex

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
2
Best Answer

Answer by sriram90 · Dec 08, 2011 at 12:39 PM

hi hatzalex,

its easy and check your moving object reached end position means just break that loop....thats it... :-)

 function moveLetters(thisTransform : Transform, startPos : Vector3, endPos : Vector3, time : float)
     {   
         var i = 0.0;
         var rate = 1.0/time;
     
     
         while(i <1.0)
         {  
            i += Time.deltaTime * rate;
            thisTransform.position = Vector3.Lerp(startPos, endPos, i);
            yield;
            if(startPos - endPos == Vector3.zero)
            {
              break;
            }
         }
     
     }
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 hatzalex · Dec 08, 2011 at 12:58 PM 0
Share

Thanks sriram90, apperantly all i needed to do is to break the loop, thanks alot mate.

$$anonymous$$odified code:

 while(i <1.0)
 {    
     i += Time.deltaTime * rate;
     thisTransform.position = Vector3.Lerp(startPos, endPos, i);
     yield;        
     
     if(!can$$anonymous$$ove)
     {
         break;
     }
 }
avatar image emrahsifoglu · Jul 04, 2013 at 06:59 PM 0
Share

I appreciate your sharing that helped me to create release behavior for button.

avatar image
0

Answer by Bicko · Dec 08, 2011 at 10:01 AM

Try printing i, see if it ever becomes >1.0.

The time calculations in your moveLetters function looks suspicious to me.. if rate is equal to 1.0 getting divided by time, then it will keep getting smaller and smaller.. and if i is Time.deltaTime multiplied by a number that's constantly getting smaller (and never >1.0) then I don't think your while statement can escape.

Comment
Add comment · Show 3 · 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 hatzalex · Dec 08, 2011 at 11:00 AM 0
Share

"i" indeed never surpasses 1, but isn't this how Lerp works? When it reaches 1, it means it reached his destination right? Anyway, i use that code snippet to ensure that the object does not slow down when it is close to the target object (code was written by Eric5h5 btw).

avatar image ks13 · Dec 08, 2011 at 11:13 AM 0
Share

distance > 5 || distance < 5...what of distance == 5?

avatar image hatzalex · Dec 08, 2011 at 11:44 AM 0
Share

While the chance the distance is exactly 5 is pretty small, i already updated the code to take also take it in consideration with no effect. Thanks for pointing it out though :)

avatar image
0

Answer by wolfomat · Oct 18, 2016 at 05:14 AM

Hello,

i've didn't read here any idea about to use this one in FixedUpdate?

you can add the lerp code in FixedPause. After you set Timer.timeScale = 0, every method in fixed timescale will be on pause. Just remember to add a code in update when you are in pause, in order to resume...

hopefully that helps, even after 5 years :)

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to smooth my camera (Vector3.Lerp) 0 Answers

Vector3.Lerp doesn't work on build 0 Answers

Smooth movement like Lerp with Touch Input? 2 Answers

Object movement with smooth start and end? 2 Answers

3 coroutines to ease in lerp, MoveToward steady pace, and ease out lerp 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