# How to perform a task in a specified time

Hello friends,

I want to make some utility classes in C# to perform the following tasks.

1. To make an object take same amount of time in moving an object from one position to another position. For example the object was at pos (0, 0, 0) and I want to move to pos (0, 0, 100) in exactly 5 secs. If change the target position to (0, 0, 1000) then also it should take the same amount of time (5 secs). If change the time instead of target position, suppose time 20 secs for target position (0, 0, 100) then i must complete the task in 20 secs instead of 5 secs.
2. As stated above you can think of rotating an object to some degrees in 5 secs, rotating an object exactly 100 times in 10 secs etc.

I am a bit confused of using Time.deltatime and Time.time in thse scenarios. Please provide your suggestions if you have.

You can use Time.deltaTime to find out the amount of time since the most recent frame. Using this you can compute the amount of distance that the object should travel. Think of it like miles per hour. If you want an object to move from (0, 0, 0) to (0, 0, 100) in 5 seconds, you could also write that as moving (0, 0, 20) per second for 5 seconds. And so you can use the following code to get the results you’re looking for.

``````void MoveObject(Transform trans, Vector3 direction, float speed)
{
trans.position += direction.normalized * speed * Time.deltaTime;
}
``````

To get the inputs for this function, of course the Transform comes from the object you’re moving. As for the direction, this can be obtained by doing the following:

``````Vector3 direction = locationToGoTo - transform.position;
``````

And lastly, the speed would be this:

``````float speed = direction.magnitude / timeToGetThere;
``````

Look into using Vector3.Lerp in a Coroutine.

Essentially you will record your to and from position, calculate your overall rate as a percentage, and then continually upDate a rate to be the sum of itself plus Time.deltaTime * yourRate, and update the position to be:

``````transform.position = Vector3.Lerp(fromPosition, toPosition, totalSumOfRates);
``````

Until the totalSum… is equal to or greater than 1 (100%).