- Home /
Synchronize object positions
I have these saws that move around those trails in pairs and at a constant speed. When I test this on my computer with good FPS it all works great and they move as intended.
But when I test it on iPad (low and oscillating FPS) the saws in each pair start to move differently, sometimes they get closer to each other and other times they get farther away. I'm having an hard time coming up with a solution for this, maybe you guys can help me.
This is my Update function:
private void Update(){
//Saw Rotation
BladeTransform.rotation *= Quaternion.AngleAxis(SawRotSpeed*360*Time.deltaTime, -transform.forward);
//Saw Movement
transform.position = Vector3.Lerp(_curStartPos, _destinyPos, _sawTimer);
if (_sawTimer < 1) _sawTimer += (Time.deltaTime*1.2f*TilesPerSec)/_distanceNextWaypoint;
else{
_sawTimer = 0;
_curStartPos = transform.position;
if (Inverse){
if (_destinyWaypoint == 0) _destinyWaypoint = WayPoints.Count - 1;
else _destinyWaypoint--;
}
else{
if (_destinyWaypoint >= (WayPoints.Count - 1)) _destinyWaypoint = 0;
else _destinyWaypoint++;
}
_destinyPos = WayPoints[_destinyWaypoint].position;
_distanceNextWaypoint = Vector3.Distance(_destinyPos, _curStartPos);
}
}
I use the common method of:
Timer = 0;
Lerp(start,end,Timer);
if(Timer<1) Timer+=Time.deltatime;
I always used this for movement but never on a situation where I needed two objects to be synchronised with each other, am I doing something wrong or is there any other way of doing this on which I can have these saws synchronised with each other?
I changed the movement to FixedUpdate() and used Time.fixedDeltaTime and it seems to have solved the problem. But I don't think this is a good solution, so if someone has any better ideas please post them.
Answer by Tomer-Barkan · Aug 27, 2014 at 02:48 PM
Why are you dividing the timer increase by _distanceNextWaypoint? That would cause them to move in different speeds depending on the length of the vector. is that what you planned? If they start in different locations, and the distance to the waypoint is different, they will move in different speeds.
Also, maybe share some more code, variable declarations, etc. Could _sawTimer be static or modifiedby other parts of code?
If still no solution, try printing it out debug values. Print out _sawTimer and (Time.deltaTime*1.2f*TilesPerSec)/_distanceNextWaypoint;
(the increase in _sawTimer).
Check that the increase is the same for all saws and the _sawTimer increases as expected every frame.
I'm dividing by _distanceNextWaypoint because I want a constant velocity.
Let's say I have these 4 WP:
WP 1 is at (0,0) WP 2 is at (2,0) WP 3 is at (2,8) WP 4 is at (0,8)
If I don't divide the timer increase by the distance to the next WP, the saw will take the same time to move from WP 1->2 and from WP 2->3, even though the distance from 2->3 is 3x higher than 1->2.
_sawTimer can't be static because each saw travels a different path, and is local to each saw.
I'm only having problems with this when the device has low and oscillating frame rate.
Here's more code:
public List<Transform> WayPoints;
public int StartWaypointIndex = 0;
public bool Inverse;
public float SawRotSpeed = 1;
public float TilesPerSec = 2;
private Vector3 _curStartPos;
private Vector3 _destinyPos;
private int _destinyWaypoint;
private float _distanceNextWaypoint;
private float _sawTimer;
// Use this for initialization
private void Start(){
_sawTimer = 0;
if (Inverse){
if (StartWaypointIndex == 0) _destinyWaypoint = WayPoints.Count - 1;
else _destinyWaypoint = StartWaypointIndex - 1;
}
else
{
if (StartWaypointIndex >= (WayPoints.Count - 1)) _destinyWaypoint = 0;
else _destinyWaypoint = StartWaypointIndex + 1;
}
_curStartPos = WayPoints[StartWaypointIndex].position;
_destinyPos = WayPoints[_destinyWaypoint].position;
_distanceNextWaypoint = Vector3.Distance(_destinyPos, _curStartPos);
}
private void Update(){
//Saw Rotation
BladeTransform.rotation *= Quaternion.AngleAxis(SawRotSpeed*360*Time.deltaTime, -transform.forward);
//Saw $$anonymous$$ovement
transform.position = Vector3.Lerp(_curStartPos, _destinyPos, _sawTimer);
if (_sawTimer < 1) _sawTimer += (Time.deltaTime*1.2f*TilesPerSec)/_distanceNextWaypoint;
else{
_sawTimer = 0;
_curStartPos = transform.position;
if (Inverse){
if (_destinyWaypoint == 0) _destinyWaypoint = WayPoints.Count - 1;
else _destinyWaypoint--;
}
else{
if (_destinyWaypoint >= (WayPoints.Count - 1)) _destinyWaypoint = 0;
else _destinyWaypoint++;
}
_destinyPos = WayPoints[_destinyWaypoint].position;
_distanceNextWaypoint = Vector3.Distance(_destinyPos, _curStartPos);
}
}
I tested with debug logs and what is happening is that when there's oscillation on the fps, if the saw blades in each pair are in different paths (one is going from WP 1->2 with a distance of 2, and the other one is going from WP 2->3 with a distance of 8) the delay on the Update function derived from the frame rate will make it so that they will move with a different speed because of their paths. Which means that this problem only happens when there's different distances between waypoints.