Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed May 21, 2019 at 07:21 PM by Jaimieself for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Jaimieself · May 21, 2019 at 06:56 PM · lerpvector2

Why doesnt Vector2.Lerp work?

I have an object that i want to Lerp left and right across the screen which is working nicely apart from the fact that is never actually reaches the destination is is coded to go to. It always stops just a little bit before it gets to where it should be.
public IEnumerator GoRight() { float elapsedTime = 0.0f;

     Vector2 currentPosition = character.transform.position;

     while (elapsedTime < overTime)
     {
         character.transform.position = Vector2.Lerp(currentPosition, new Vector2(currentPosition.x - 1000, transform.position.y), elapsedTime/overTime);
         elapsedTime += Time.deltaTime;
         yield return null;
     }

     currentPosition = character.transform.position;
 }  

So basically I want to move -1000 from the current position but it usually moves about 980 to 990 meaning the object is always off centre.

Is there a way to get it to Lerp exactly 1000 every time?

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

  • Sort: 
avatar image
1
Best Answer

Answer by xxmariofer · May 21, 2019 at 07:06 PM

never reaches the destitnation because the exact iteration when elpasetime is 1 stops the while before calling the lerp method

  public IEnumerator GoRight() {
    Vector2 currentPosition = character.transform.position;
    elapsedTime = 0;
    while (elapsedTime <= overTime)
    {
        elapsedTime += Time.deltaTime;
        character.transform.position = Vector2.Lerp(currentPosition, new Vector2(currentPosition.x - 1000, transform.position.y), elapsedTime/overTime);
  
        yield return null;
    }
    currentPosition = character.transform.position;
 }
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 Jaimieself · May 21, 2019 at 07:13 PM 0
Share

Ah O$$anonymous$$ that makes sense. Any idea how that can be fixed? I'm going to have to experiment a bit more i guess.

avatar image xxmariofer Jaimieself · May 21, 2019 at 07:15 PM 0
Share

did you try the code i posted? should fix it

avatar image Jaimieself xxmariofer · May 21, 2019 at 07:21 PM 0
Share

Oh thanks. Thats perfect.
Sorry, i didn't actually spot a change until you said that.
Its working perfectly now, Thanks again.

Follow this Question

Answers Answers and Comments

107 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Vector3.Lerp result in "laggy" movement while running on iOS devices 2 Answers

Looking to see if the method I'm using for movement is correct or if I'm doing something wrong. 0 Answers

Difficulty with Vector2 Lerp, object jumping, not moving 2 Answers

Moving an UI element from point A to point B 1 Answer

Vector2 Lerp alternative? 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