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 /
avatar image
0
Question by Noshiz · Aug 26, 2018 at 01:02 PM · 2dmovementsmoothwaypoints

Smooth movement between waypoints 2D

Hi, I'm trying to achieve smooth movement between Vector2 waypoints (in 2D) without either jumping ahead or "lagging" behind because for 2 frames you can move to the same spot while the waypoints are being swapped. I've been struggling with the logic required for it for the past 2 days now.


This is my code:

         t += Time.deltaTime * speed;
         float nextFrame;
         Vector2 nextFramePosition;
 
         if (t < 1f)
         {
             transform.position = Vector2.Lerp(wayPointList[0], wayPointList[1], t);
 
             nextFrame = t + Time.deltaTime * speed;
             nextFramePosition = Vector2.Lerp(wayPointList[0], wayPointList[1], nextFrame);
 
             if (nextFramePosition == wayPointList[1])
             {
                 t = 0;
                 transform.position = Vector2.Lerp(wayPointList[0], wayPointList[1], nextFrame);
                 endOfWaypoint = true;
             }
 
         }


In the part where I "predict" the position in the next frame I can either set that position early (which causes a jump ahead to the waypoint) or If I don't it will still jump ahead on the next frame because t will be higher than 0 so it has a head start. And if I change the speed to a higher value the "head start" is even higher which is expected but not desirable.


Is there a spesific technique to tackle something like that ? And yes I am aware of the assets that can offer that but I'm trying to write my own implemetation.

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

Answer by eses · Aug 26, 2018 at 01:27 PM

Hi @Noshiz

You description doesn't make Lerping sound like a good choice. If your target can change at any moment, why not just make your movement based on MoveTowards?

See manual for example:

https://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html

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 Noshiz · Aug 26, 2018 at 02:06 PM 1
Share

Your suggestion was actually good. Reworked a bit the code and the movement is much smoother now.

         if (Vector2.Distance(transform.position, wayPointList[0]) <= 0.5)
         {
             endOfWaypoint = true;
         }
 
         if (endOfWaypoint)
         {
             endOfWaypoint = false;
             wayPointList.RemoveAt(0);
         }
 
         transform.position = Vector2.$$anonymous$$oveTowards(this.transform.position, wayPointList[0], Time.deltaTime * speed);

I'm checking the distance now ins$$anonymous$$d of the exact positions and move on to the next waypoint based on that. I am however wondering how well that will work with curves.

avatar image eses Noshiz · Aug 26, 2018 at 02:09 PM 0
Share

ok good - if the answer is correct, consider marking it as accepted / best answer. Thanks

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

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

2D smooth top-down movement 1 Answer

Unity 2D - Smooth movement over tiles?? 1 Answer

Complex 2D movement (X,Y) using velocity. 1 Answer

Android 2d movement/waypoint script issues 0 Answers

Smooth Character Movement 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