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 Sonaten · Nov 12, 2012 at 10:55 PM · lerpvector3.lerp

Computation of Vector3.Lerp, does it check for change?

A simple question. If I have objects that use

 transform.position = Vector3.Lerp(start.position, end.position, Time.time);

in void Update(). Would I need to check if the current position of the object is equal to the destination to stop the execution of the interpolation, or does the Vector3.Lerp already handle this?

EDIT: I have the function working as intended, I am just wondering if I should make sure that it isn't called when it isn't needed.

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

Answer by Memige · Nov 12, 2012 at 11:09 PM

Lerp will only give an interpolation ranging from the start to the finish based on that third parameter (0 being the start, 1 being the finish) so it will never go past the end point. However, as a note, since Time.time is seconds since the application started, it is likely over 1, which is getting capped, and instantly snapping your object to the end position. Instead try a variable starting at 0 and incrementing by Time.deltaTime every frame. this should give you a smooth movement from one to the other.

--EDIT--

To clearly answer the question: No Lerp does not stop executing when you reach your destination. You will want to put in logic to check for reaching the destination, and skipping the lerp call when it is no longer needed.

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 Sonaten · Nov 12, 2012 at 11:16 PM 0
Share

This is not what I am looking for. I know the functions third parameter has to within the range 0 to 1. What I am asking for is, when we reach passed 1, does it stop calculations? or how does Lerp handle it. Would it be clever of me to put an ifstructure to check if end destination has been reached, or would that be 'doubling the effort'?. I am currently just calculating a value ins$$anonymous$$d of Time.time, so there is no problem there. The interpolation is working fine.

 void Update () {
     LerpVal += Time.deltaTime;
     if(isShow == false) LerpHide();
     else LerpShow();
     if(LerpVal > 1) LerpVal = 1;
 }

 private void LerpShow()
 {
     transform.position = Vector3.Lerp(hiddenPos, shownPos, LerpVal*0.9f);
 }
avatar image Memige · Nov 12, 2012 at 11:19 PM 0
Share

Ok, my apologies. I do not know if there are optimizations inside the function to skip the process if the third param is 0 or 1, but the function does still execute, so yes, throwing in a check to stop calling it when you reach the destination would help with efficiency.

avatar image Sonaten · Nov 13, 2012 at 11:34 PM 0
Share

This, or get a grip of coroutines as Erich5h5 suggested, and use it to control if it is called or not :) Thanks for the help

avatar image Memige · Nov 13, 2012 at 11:36 PM 0
Share

Sure ^^ and I agree, the advice of Eric is good stuff

avatar image
1

Answer by Eric5h5 · Nov 13, 2012 at 12:41 AM

You should generally not use Update if you want something to happen only some of the time, since Update always runs every frame (and has overhead each time it's called). Use a coroutine instead, that way you can make it stop running when you're done.

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 Sonaten · Nov 13, 2012 at 11:32 PM 0
Share

I am not at all familiar with the use of coroutines I'm afraid. But it seems like an important aspect of Unity. So I will start researching it, and try to get my head around it.

avatar image
1

Answer by Owen-Reynolds · Nov 13, 2012 at 12:59 AM

Lerp is just a simple math function. There's pretty much one way to write it, and you can decide for yourself when to skip it:

 Vector3 Lerp(Vector3 P1, Vector3 P2, float pct) {
   if(pct<=0) return P1;
   if(pct>=1) return P2;
   return P1 + (P2-P1)*pct;
 }
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 Sonaten · Nov 28, 2012 at 11:26 AM 0
Share

1up for the 'inside'-info. Additionally; I was also able to use this in another scenario where I only needed to interpolate a single value from one to another.

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

12 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

Related Questions

How the heck does Mathf.Lerp work? 2 Answers

make my gameobject lerp between points more smoothly 0 Answers

I'm using TRANSLATE but want to use LERP. C# 2 Answers

vector3 lerp based on 2 different floats/axes? 1 Answer

Vector3.Lerp move weapon up to players face 2 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