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
4
Question by AntLewis · Dec 06, 2011 at 09:53 AM · coroutinelerp

Coroutines and Lerp - how to make them friends?

Hi there, so I've spent a bit of my time getting my head around coroutines which is a fantastic tool to have in your armoury. However, I'm confused by how to use a Vector3.Lerp within a coroutine - separately, I understand how they work, but I'm a little fuzzy as to how to make them work together. I've been pointed to a javascript piece of code here but this doesn't make things much clearer for me. As always, any help is truly appreciated.

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

Answer by syclamoth · Dec 06, 2011 at 09:57 AM

The two functions are completely orthogonal- there's no reason whatsoever why they can't work together!

In fact, they are particularly useful in conjunction. Here's a simple bit of code for making an object move to a new position over a period of n seconds.

 function MoveToPosition(newPosition : Vector3, time : float)
 {
     var elapsedTime : float = 0;
     var startingPos : Vector3 = transform.position;
     while (elapsedTime < time)
     {
         transform.position = Vector3.Lerp(startingPos, newPosition, (elapsedTime / time));
         elapsedTime += Time.deltaTime;
         yield;
     }
 }

Simply call that function with StartCoroutine, and it'll all work!

 StartCoroutine(MoveToPosition(new Vector3(0, 10, 0), 5));
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 AntLewis · Dec 08, 2011 at 02:09 PM 0
Share

Thanks for the response - fantastic!

avatar image Jovy83 · Dec 17, 2015 at 06:35 PM 0
Share

I do like to point out that by using this code, the position where the object ends up may be inconsistent. For example, I needed my Gameobject to stop at exactly 1.12345 in the yPos. This code does not guarantee that since one possible reason is that elapsedTime < duration doesn't necessarily always break on exactly the same elapsedTime value. Just a heads up for future devs who run into this thread searching for answers as well :))

avatar image purpl3grape Jovy83 · Jan 15, 2017 at 01:20 AM 0
Share

Did you try: At the end of path, just sync position?

avatar image
16

Answer by ilezhnin · Nov 14, 2014 at 05:46 PM

And for C#

     private IEnumerator FadeOut(float alphaStart, float alphaFinish, float time)
     {
       if (bgTexture == null)
         yield return null;
 
      float elapsedTime = 0;
      bgTexture.alpha = alphaStart;
 
      while (elapsedTime < time)
      {
        bgTexture.alpha = Mathf.Lerp(bgTexture.alpha, alphaFinish, (elapsedTime / time));
          elapsedTime += Time.deltaTime;
        yield return new WaitForEndOfFrame();
      }
     }
Comment
Add comment · Show 4 · 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 robertbu · Nov 14, 2014 at 05:47 PM 2
Share
  yield return null;
avatar image ElectricMonk · Jul 03, 2015 at 01:46 AM 2
Share

I noticed the WaitForEndOfFrame() as well. Is there an advantage of that over yield return null?

avatar image b1naryatr0phy · Aug 13, 2015 at 10:31 PM 1
Share

@Electric$$anonymous$$onk

They simply return at different points during a frame. WaitForEndOfFrame() obviously returns at the end of the frame. Returning null will return during main coroutine updates (which I believe occurs between Update() and LateUpdate(), which also means it occurs before rendering.)

Also note that WaitForEndOfFrame() is a class object, so caching it in a variable and reusing it will create slightly less overhead in performance-heavy operations.

avatar image TooManySugar · Sep 17, 2017 at 08:22 PM 1
Share

I was placing the yeld return OUTSIDE the while, and It was behaving very weirdly. Now yous aved my life cause I burnt half afternoon trying to figure out the hell was going on. Can you explain why it needs to be inside the while, even further if it returns nothing?

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

Lerping inside Coroutine occurring too quickly 1 Answer

lerp in coroutine doesn't work correctly 0 Answers

restrict Lerp to only one axis? 1 Answer

Lerping multiple materials of a gameobject within a coroutine 0 Answers

Lerping blinks 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