• 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 KronosAmon · Aug 08, 2019 at 11:24 AM · movementvector3lerp

Vector3 Lerp finish too quicly

I am trying to write a simple script which moves the player from one position to another position.
I used a list of GameObject for the player to use these objects as the destination points.
For this I used Vector3.Lerp and an IEnumerator.
.
In documentation Lerp have a float parameter which has a range of 0 to 1;
As I understand when this parameter becomes 1 that's mean object arrives at its destination. (This is not happening in my code which is my problem)
So I tried to write a code that makes the player move to next point and when it arrives then immediately starts to make it move to the next point until there is no next point left.

     //
     public List<GameObject> WayPoints = new List<GameObject>();
     public float speed = 10f;
     private int nextWayPoint = 0;
 
     private void Start()
     {
         StartCoroutine(SmoothlyShiftPosition(WayPoints[nextWayPoint].transform.position, speed));
     }
 
     IEnumerator SmoothlyShiftPosition(Vector3 destination, float OverTime)
     {
 
         nextWayPoint++;
         float startTime = 0f;
         while (startTime / OverTime < 1f)
         {
             Debug.Log(startTime / OverTime);
             transform.position = Vector3.Lerp(transform.position, destination, startTime / OverTime);
             startTime += Time.deltaTime;
             yield return null;
         }
         transform.position = destination;
 
         if (WayPoints[nextWayPoint] != null)
         {
             StartCoroutine(SmoothlyShiftPosition(WayPoints[nextWayPoint].transform.position, speed));
         }
     }
     

My problem is the player arrives at next point long before (startTime / OverTime) parameter becomes "1". And player waits until the parameter finally becomes "1" and then start to go to the next point.
How can I fix this?

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 Hellium · Aug 08, 2019 at 11:31 AM

If you use Lerp( A, B, t ) with t ∈ [0, 1], A and B must remain the same, however, in your current code, A is changed every frame.

  IEnumerator SmoothlyShiftPosition(Vector3 destination, float OverTime)
  {
      Vector3 origin = transform.position;
      nextWayPoint++;
      float startTime = 0f;
      while (startTime / OverTime < 1f)
      {
          Debug.Log(startTime / OverTime);
          transform.position = Vector3.Lerp(origin, destination, startTime / OverTime);
          startTime += Time.deltaTime;
          yield return null;
      }
      transform.position = destination;
 
      if (nextWayPoint < WayPoints.Count && WayPoints[nextWayPoint] != null)
      {
          StartCoroutine(SmoothlyShiftPosition(WayPoints[nextWayPoint].transform.position, speed));
      }
  }
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 KronosAmon · Aug 08, 2019 at 11:38 AM 0
Share

Thanks a lot, it worked. I should have been more careful.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

183 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 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

Stopping Vector3.Lerp 3 Answers

Vector3.Lerp doesn't work on build 0 Answers

3 coroutines to ease in lerp, MoveToward steady pace, and ease out lerp 0 Answers

Smooth movement like Lerp with Touch Input? 2 Answers

Object movement with smooth start and end? 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