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 ziadbha · Jul 08, 2017 at 06:44 PM · c#lerptranslatemoving platformwaypoint system

Vector2.Lerp not working properly. Horizontal jittering when not moving horizontally at all!

Hi everyone. I'm making a moving platform system. The way it works is that I set up waypoints for the platforms, and it lerps between the points. The velocity is then the newPosition (the lerped one) - transform.position. I use that velocity in transform.Translate().

Here is the problem. I currently have 2 waypoints. One at (0,0) (center of box) and the other directly above it. The newPosition computed by lerp is correct, but the translation is jittery, and the object seems to move horizontally a bit (perhaps a floating point error?)

Here is a gif to illustrate this: https://gyazo.com/3c0c24c126b2e356f563a6e0d5e2ca36

Notice how current position is supposed to be at -26 but it goes to -25.9 and -26.1 sometimes.

This ONLY happens when the object is rotated a bit. When it is fully rotated (180), it goes crazy and starts flying off. I'm not sure why it is happening.

Here is the waypoint code (which I basically followed from a youtube tutorial)

 Vector2 CalculatePlatformMovement() {
             if (Time.time < nextMoveTime) {
                 return Vector2.zero;
             }
             fromWaypointIndex %= globalWaypoints.Length;
             int toWaypointIndex = (fromWaypointIndex + 1) % globalWaypoints.Length;
             float distanceBetweenWaypoints = Vector2.Distance (globalWaypoints [fromWaypointIndex], globalWaypoints [toWaypointIndex]);
             percentBetweenWaypoints += Time.deltaTime * speed/distanceBetweenWaypoints;
             percentBetweenWaypoints = Mathf.Clamp01 (percentBetweenWaypoints);
             float easedPercentBetweenWaypoints = Tools.Ease (percentBetweenWaypoints, easeFactor);
     
             Vector2 newPos = Vector2.Lerp (globalWaypoints [fromWaypointIndex], globalWaypoints [toWaypointIndex], easedPercentBetweenWaypoints);
     
             if (percentBetweenWaypoints >= 1) {
                 percentBetweenWaypoints = 0;
                 fromWaypointIndex ++;
     
                 if (!cyclic) {
                     if (fromWaypointIndex >= globalWaypoints.Length-1) {
                         fromWaypointIndex = 0;
                         System.Array.Reverse(globalWaypoints);
                     }
                 }
                 nextMoveTime = Time.time + waitTime;
             }
             print ("current pos: " + (Vector2)transform.position);
             return (newPos - (Vector2) transform.position);
         }
 

This method is called in the Update() method, like this:

 void Update () {
         UpdateRaycastOrigins ();
         velocity = CalculatePlatformMovement(); //update the velocity based on the movement vector
         CalculatePassengerMovement(velocity); //Check collisions with passengers here, add to list of passengers
 
         MovePassengers (true); //Do all movement of passengers that move BEFORE the platform moves
         transform.Translate (velocity); //Move the platform
         MovePassengers (false); //Do all movement of passengers that move AFTER the platform moves
     }


In the start method, I convert the local waypoints to world global waypoints, like so:

 public override void Start () {
         base.Start ();
         globalWaypoints = new Vector2[localWaypoints.Length];
         for (int i = 0; i < localWaypoints.Length; i++) {
             globalWaypoints [i] = localWaypoints [i] + (Vector2) transform.position;
         }
     }




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
0

Answer by springwater · Jul 08, 2017 at 06:54 PM

Have you checked for parent child issues with other objects in the scene, perhaps its following parental movement.

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 ziadbha · Jul 08, 2017 at 07:00 PM 0
Share

The moving platform is it's own parent. There are no children attached to it. Here is a gif that shows that only when the object is rotated the problem appears:

No rotation: https://gyazo.com/03292c813a4157da311b58d2af475d05

10 degree rotation: https://gyazo.com/d2e95725b0a438ecd6c63a1ac3a84f65

Focus on the x coordinate of the position. In the good version it is fixed at -26, while in the angled version it jitters around it...

avatar image springwater · Jul 09, 2017 at 03:57 PM 0
Share

It occurred to me that perhaps it has a collider and is hitting the other object/s in the scene. I believe, you can restrict movement on certain axes in the physics tab. in your code, you have, transform,translate (velocity) velocity is described in the calculate platform movement function. As x,y position when it says "return (newPos - (Vector2) transform.position);" So if the moving object isn't at zero position on one of the two axes, there will be movement applied to both. So when it rotates, perhaps, the forward velocity changes the coordinate that should be zero to non zero.. you can fix it, buy telling it to translate like this.. Transform.translate(x,y,z); where 2 of those three variable should be zero every time for straight movement on one axis.

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

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

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Using Mathf.LerpAngle to gradually rotate an objects x position 1 Answer

Lerping in One Direction 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