Of course the are many many ways to do that, given the very vague input this would be a generic animation routine for moving an object between startPoint
and endPoint
using the given step parameters
public static void MoveFromTo(this MonoBehaviour obj, Vector3 startPoint, Vector3 endPoint, float stepSize, int stepsToUse, float stepDuration)
{
obj.StartCoroutine(MoveRoutine(obj.transform, startPoint, endPoint, stepSize, stepsToUse, stepDurarion));
}
private static IEnumerator MoveRoutine(Transform obj, Vector3 startPoint, Vector3 endPoint, float stepSize, int stepsToUse, float stepDuration)
{
obj.position = startPoint;
for(var i = 0; i < stepsToUse; i++)
{
yield return new WaitForSeconds(stepDuration);
obj.position = Vector3.MoveTowards(obj.position, endPoint, stepSize);
if(obj.position == endPoint) break;
}
obj.position = endPoint;
}
Where
obj
the object to move, will also be running the routine
startPoint
the position to start at
endPoint
the theoretical target position
stepSize
how far to move per step
stepsToUse
up to how many steps to use
stepDuration
delay in seconds before the next step
Clamping is applied so if the endPoint
is already reached with less than the given steps, no more steps are done.