Mathematicle error help

I’m having a little issue tracking down a mathematical error in my script code. I’ve developed a simple script to move a cube around my map in a rectangular fashion, however when the cube finishes it circuit the cube ends up in a different position to where is started.

The cube starts on the z axis at 716.8967 and on the x axis at 672.9058. Then the cube moves across the z axis for 140 frames (going 5 on Vector3) then goes up on the x axis for 115 frames(speed stays the same). Then the opposite happens and the cube finishes at 716.1387 (z axis) and 671.6195 (x axis).

Could anyone help me understand where I lost 0.758 on the z axis and 1.2863 on the x axis. I’ve placed the code bellow (Don’t ask about the names). Thank you!

   #pragma strict
    var x = 0;//x axis
    var z = 0;//z axis
    var R = true;//right
    var L = false;//left
    var U = false;//up
    var D = false;//down
    function Update () {
    	//Makes Alan Rickman move along Z axis
    	transform.Translate(Vector3(5,0,0) * Time.deltaTime);
    	Debug.Log("Alan is moving Right");
    		if(z > 139){
    		R = false;
    		U = true;
    		var gary = Resources.LoadAssetAtPath("Assets/Textures/garyoldman.jpg", Texture);
    		renderer.material.mainTexture = gary;
    	//Makes Alan Rickman move along X axis
    	transform.Translate(Vector3(0,0,5) * Time.deltaTime);
    	Debug.Log("Alan should be moving up");
    		if(x > 114){
    		U = false;
    		L = true;
    		var Kevin = Resources.LoadAssetAtPath("Assets/Textures/kevin_spacey.jpg", Texture);
    		renderer.material.mainTexture = Kevin;

//Makes Alan Rickman move along Z axis
transform.Translate(Vector3(-5,0,0) * Time.deltaTime);
Debug.Log("Alan is moving left");

	if(z < 1){
	L = false;
	D = true;
	var jackson = Resources.LoadAssetAtPath("Assets/Textures/jackson-main.jpg", Texture);
	renderer.material.mainTexture = jackson;

//Makes Alan Rickman move along X axis
transform.Translate(Vector3(0,0,-5) * Time.deltaTime);
Debug.Log("Alan Should be moving down");

	if(x < 1){
	D = false;
	R = true;
	var alan = Resources.LoadAssetAtPath("Assets/Textures/alan_rickman.jpg", Texture);
	renderer.material.mainTexture = alan;


You’re multiplying the movement distance by Time.deltaTime.

transform.Translate(Vector3(5,0,0) * Time.deltaTime);

This isn’t a constant value, but rather the time the last frame took to update. So the amount you’re moving isn’t constant.

Trying using the FixedUpdate function rather than Update. That should give you a constant time.

Also, a better way to do this would be to decide the waypoints you want to use, then use Vector3.Lerp to move between them. That way you’re absolutely guaranteed to always move to the same place each time.