- Home /
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;
}
}
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.
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...
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
Follow this Question
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