Time difference between the Editor and iPhone

I’m working on a drag racing game where accurate lap times are extremely critical.

I have the AI cars tuned to complete laps at specific times in the the Unity Editor, but on the iPhone the lap times are ~0.4 seconds slower.

For example, in the editor the car runs a lap in 10.916 seconds. On the iPhone that car runs the lap in 11.296 seconds. 0.380 seconds difference. (I know this doesn’t sound like a big deal, but in drag racing, that’s a lot of time.)

I can’t seem to figure out what would cause the timing difference between the 2 platforms. I’ve tried tuning the Timestep to see if that would make a difference, but there’s some black magic going on in those settings that I don’t understand. I tried limiting the framerate in the editor to match the iPhone, but the times in the Editor were still as I tuned them. And I tried tuning the rendering loop, but the slower times on iPhone remain.

Any thoughts on where else I should look to figure out the difference?

Drag racing is in a straight line? If the cars are going straight maybe you could do something like this -

Say the car’s going down its z axis in the positive direction, and the track length is 40 world units. You want the car to take 10.916 seconds to get there. For the sake of argument and to make the math easier let’s say the car starts as world Vector3(0, 0, 0) and the track goes straight down the world z axis.

In a late update see what the how many seconds (in float format) have passed since the race started. You could set a var, maybe startTime, to the value of Time.time is at the moment the race starts.

Say after the race starts in the first lateUpdate Time.time - startTime = 1.2345. That’s 11.3090 percent of the time that the trip should take - 100(1.2345)/10.916.

11.3090 percent of the distance, 11.3090*40/100, is 4.5236 units. In this first update the car’s z position is 0, so you could use transform.Translate(Vector3(0, 0, 4.5236), Space.self) to get it there.

In the next update let’s say Time.time – startTime = 2.1234. That’s 19.4521 percent of the time and means the car should be that percent of the way down the track – 7.7808 units. Due to our translation in the last frame the car is now at z 4.5236, so it needs to transform.Translate(Vector3(0, 0, 3.2572), Space.self) because 7.7808 – 4.5236 = 3.2572.

Repeat until the car is where it needs to be.

I’ve not tried any of this, kinda stream of consciousness, but it should work (more or less) I think. I do doubt that you’ll be able to get exact precision across platforms to the thousandths decimal place, but I don’t know for sure.

Hope my babbling makes since and is of some help.

I had to know it it worked at all, it pretty much does. Maybe not exactly what you’re looking for, but more or less as I understood it. No interpolation needed with a decent frame rate.

Of course there’s no easing; the car goes at the same speed the whole time.

Attached project has a plane and a cube, cube reps the car.

private var startTime : float;
var tTime : float;
var tDist : float;
var car : GameObject;

function Start()
{
	starTime = Time.time;
}

function LateUpdate ()
{
	var tPct : float = ((100 * (Time.time - startTime)) / tTime);
	var dPct : float = ((tPct * tDist) / 100);
		
	if (car.transform.position.z < tDist)
	{
		car.transform.Translate(Vector3(0, 0, (dPct - car.transform.position.z)));
	}
}

function OnGUI()
{
	GUI.Label(Rect(8, 8, 128, 32), Time.time.ToString());
}

201020–7418–$timedist_152.zip (470 KB)

Thanks for taking the time. This is an interesting take indeed, one which I had not considered.

I probably should have noted that I’m taking a more physical/real-world approach to the starting and stopping of the timer. I start the clock when the player presses the gas and I stop the clock when the car crosses the finish line. :slight_smile:

My car is a rigidbody being driven by wheelcolliders. Everything works perfectly and I have very fine control over the tuning of the cars so I can make the AI cars run whatever lap times I want.

Sigh… Except for the ~0.4 second difference on the iPhone. Near as I can tell, it’s not a framerate issue since I can bring the framerate in the editor to below iPhone performance and the timing is still exact. My cars are running in FixedUpdate, naturally. However I’m using time instead of deltaTime on my clocks. I dunno if that would make this slight difference or not. Hmmm… Or maybe the trigger colliders could be flaky??..