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 Inan-Evin · Feb 05, 2015 at 04:28 AM · coroutinetimelerp

Lerp to position, after a while come back, problem occured using Coroutines

Hello everyone, I have a gameobject whose script has 2 coroutines for lerping. When this game object is activated, it takes object A and sets its position & rotation to object X's position & rotation. And then, it starts a coroutine. This coroutine lerps object A to a desired position, also it lerps it's rotation too. And when I press F key, it calls another coroutine. Which takes the object A to X's position and rotation back. But I have a problem, first lerp happens without a problem, but the second one has problems. It starts too slow, goes to wrong position, and suddenly in a split of a second it goes to where it is supposed to go. It's not logical, I could not understand the problem. Both coroutines have different float values in order to determine the time. I think problem is about the calculations of these. Just look at the statistics and informations above and then the code itself.

First Lerp Coroutine Time : 2 or around - It works okay, if I decrease the value it fastens which is okay. First Lerp Coroutine Time : 5,6,7,8,9,.... - It looks like after some value it does not slow down the lerping, couldn't understand why.

Second Lerp ( lerping back to first position & rotation ) Coroutine Time : 2 or around - It works okay, but if I put anything bigger than 2-3-4 or something like that it does not work and the problem I told you above starts to occur.

Here are my lines :

 IEnumerator Lerp(float time)
     {
         
         float elapsedTime = 0.0f;
 
         while(elapsedTime < time)
         {
             
             
             cameraHolder.localPosition = Vector3.Lerp(cameraHolder.localPosition, referanceTransform.localPosition, elapsedTime / time);
             cameraHolder.localRotation = Quaternion.Lerp(cameraHolder.localRotation, referanceTransform.localRotation, elapsedTime / time);
             elapsedTime += Time.deltaTime;
             yield return new WaitForEndOfFrame();
         }
     }

 // I call the first lerp like this where FirstTime is a float StartCoroutine(Lerp(FirstTime));

     IEnumerator LerpBack(float time)
     {
         float elapsedTime = 0.0f;
 
         while (elapsedTime < time)
         {
             cameraHolder.localPosition = Vector3.Lerp(cameraHolder.localPosition, playerCamReferance.localPosition, elapsedTime / time);
             cameraHolder.localRotation = Quaternion.Lerp(cameraHolder.localRotation, playerCamReferance.localRotation, elapsedTime / time);
 
             elapsedTime += Time.deltaTime;
             yield return new WaitForEndOfFrame();
         }
     }

 // I call this one like this where SecondTime is a float StartCoroutine(LerpBack(SecondTime));

PS. I do not have anything wrong with the position I set as a parameters for lerp functions. I double checked them with debugging, nothing is wrong. When I mess with the time values, really weird results can occur in the second lerp. Any ideas ? Thanks :).

EDIT : I debugged the elapsedTime value and it reaches to the given time long after the lerp is visually finished.

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

1 Reply

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

Answer by Patel-Sagar · Feb 05, 2015 at 07:56 AM

Hey Inan,

If you want your cameraHolder to reach at destination in exact time, then you have to pass 'start localPosition' in Lerp function not 'cameraHolder.localPosition'.

so your code would be like,

 IEnumerator Lerp(float time)
 {
     float elapsedTime = 0.0f;
     Vector3 startLocalPosition = cameraHolder.localPosition;
     Vector3 startLocalRotation = cameraHolder.localRotation;
     while(elapsedTime < time)
     {
         cameraHolder.localPosition = Vector3.Lerp(startLocalPosition, referanceTransform.localPosition, elapsedTime / time);
         
         cameraHolder.localRotation = Quaternion.Lerp(startLocalRotation, referanceTransform.localRotation, elapsedTime / time);
         
         elapsedTime += Time.deltaTime;
         yield return new WaitForEndOfFrame();
     }
 }



and also you can do the same for second Ienumerator. I have written startLocalPosition for your understanding. Maybe starting position is already saved with you in playerCamReferance.localPosition. if that is the case then replace that with startLocalPosition in my code. Let me know if you have any query.

Regards.

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 Inan-Evin · Feb 06, 2015 at 03:26 AM 0
Share

Oh so that was the problem, because it is in while loop and coroutine, I have to store the first position and rotation in Vector3 and Quaternion, alright, thanks !

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

20 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

Related Questions

Why is my Mathf.Lerp not lasting for the intended duration, even though I'm not using Time.time as the t variable? 1 Answer

slow a lerp ? array/static problem 1 Answer

Jump with mathf.lerp problem 2 Answers

how to use time on lerp 2 Answers

Starting and stopping Coroutines 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