Error in the total time needed to translate a GameObject

Hi there,

I’m translating a GameObject one unit length. By the physics formula it should takes one second.

175836-eq16avev.gif

Here’s the code

public class Testing : MonoBehaviour
{
	[Tooltip("In units per second")]
	public Vector2 displacement;

	float currentTime;

	Stopwatch sw = new Stopwatch();

	// Start is called before the first frame update
	void Start()
	{
		currentTime = 0;
		sw.Start();
	}

	// Update is called once per frame
	void Update()
	{
		if (currentTime >= 1.0f)
		{
			sw.Stop();
			UnityEngine.Debug.Log($"Time to translate: {sw.ElapsedMilliseconds}");
			return;
		}

		Vector2 currentPos = transform.position;
		var nextPos = currentPos + (displacement * Time.deltaTime);

		transform.position = nextPos;

		currentTime += Time.deltaTime;
	}
}

when currentTime counter reach 1 the GameObject is indeed in the expected position so I thought everything was fine but I added a Stopwatch to measure the time it takes and for my surprise it is 1324 milliseconds.

Can anyone please give me some clue about why is this happening?

Thank you

Maybe, instead of starting your sw on start, start it in the update if currentTime == 0.

     void Start()
     {
         currentTime = 0;
     }
 
     // Update is called once per frame
     void Update()
     {
         if (currentTime == 0)
         {
              sw.Start();
         }

         if (currentTime >= 1.0f)
         {
             sw.Stop();
             UnityEngine.Debug.Log($"Time to translate: {sw.ElapsedMilliseconds}");
             return;
         }
 
         Vector2 currentPos = transform.position;
         var nextPos = currentPos + (displacement * Time.deltaTime);
 
         transform.position = nextPos;
 
         currentTime += Time.deltaTime;
     }