• 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
6
Question by ina · May 07, 2011 at 09:03 AM · transformpositionvector3updatelerp

Vector3.Lerp works outside of Update()

Does Vector3.Lerp() for transforming position work outside of update?

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
35

Answer by superpig · May 07, 2011 at 11:50 AM

It's a plain old function, so it 'works' anywhere - in that it will correctly calculate a value that is the linear interpolation between the start and end values. I use it, for example, in my AI code to compute how 'audible' a sound is based on how far away it is.

However, most of the time, when people are using Lerp(), they're trying to achieve smooth movement from point A to point B. To get smooth movement, you need to recompute the position very frequently - every frame, if possible.

Update() is called every frame, so you could call it there. You could also write a coroutine that does work every frame and calls it:

IEnumerator MoveObject(Vector3 source, Vector3 target, float overTime)
{
    float startTime = Time.time;
    while(Time.time < startTime + overTime)
    {
        transform.position = Vector3.Lerp(source, target, (Time.time - startTime)/overTime);
        yield return null;
    }
    transform.position = target;
}

The advantage of doing it in a coroutine is that when the object isn't moving, the coroutine isn't running, which can save Unity a bit of time; by comparison, if you put the code in Update, Unity will always call Update, even if you end up not doing anything for the frame.

Also, with the coroutine approach, you can (if you wish) trade smoothness for framerate by yielding a WaitForSeconds() object instead of null. The longer you WaitForSeconds(), the jerkier the movement will be, but the less frequently Unity will have to do the computation.

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 Barrett-Fox · Jun 17, 2013 at 10:16 PM 2
Share

This question and answer helped me. But I also needed to know about calling an IEnumerator function with StartCoroutine as explained here.

avatar image Shippety · May 13, 2016 at 12:59 AM 0
Share

Just what I was looking for! Thanks so much

avatar image mmsdaus · Nov 10, 2019 at 11:26 AM 0
Share

8 years later, this works, thank you @superpig

avatar image
5

Answer by Mike 3 · May 07, 2011 at 10:07 AM

It works wherever you want it to, it's a simple mathematical function, min + (max - min) * t

You can generally use it well in coroutines, where you have a fixed start and end, and increment the t value from 0 to 1

Quick example for a coroutine:

IEnumerator MoveTo(Vector3 position, float time) { Vector3 start = transform.position; Vector3 end = position; float t = 0;

 while(t &lt; 1)
 {
     yield return null;
     t += Time.deltaTime / time;
     transform.position = Vector3.Lerp(start, end, t);
 }
 transform.position = end;

}

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 ina · May 07, 2011 at 10:24 AM 0
Share

can you give an example of how it might work with a coroutine?

avatar image Mike 3 · May 07, 2011 at 09:39 PM 0
Share

done - i haven't checked it, but looks reasonable enough at second glance

avatar image
-3

Answer by Meltdown · May 07, 2011 at 09:36 AM

No, it is meant to be used within the Update() method.

Comment
Add comment · Show 7 · 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 ina · May 07, 2011 at 09:47 AM 0
Share

so lerp does not work at all in a function outside? what if update calls a function outside with lerp in it

avatar image Eric5h5 · May 07, 2011 at 10:29 AM 2
Share

It is not meant to be used in Update. In fact it's generally better outside Update. http://answers.unity3d.com/questions/6949/can-someone-explain-how-using-time-deltatime-as-t-in-a-lerp-actually-works/6950#6950

avatar image Meltdown · May 07, 2011 at 11:18 AM 0
Share

ina, sure, Update() can call any external method. But any method that Update() calls will still run the context of the Update() method execution. Eric thanks for the informative post, interesting stuff!

avatar image superpig ♦♦ · May 07, 2011 at 11:44 AM 0
Share

Eric, I read your link but couldn't see from your answer why it's bad to call Lerp() inside Update(). Could you elaborate?

avatar image Mike 3 · May 07, 2011 at 09:45 PM 1
Share

people tend to misuse it a lot, lerping from the current position to the target position, which ends up with a weird almost exponential decay movement, which is dependent on frame rate.

Show more comments

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Lerp not working 1 Answer

[2D] Get the position of an object outside the scope 2 Answers

transform.position = new Vector 3 NOT moving to correct position? 1 Answer

How do I freeze a gameObject when MouseDrag runs over a certain Vector3? 1 Answer

Issues with local position and positions 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