Getting a Gameobject to move position to another over time?

I’m having trouble figuring out how to make it where I can make a gameobject go from it’s initial position to target position over time. Any idea how? Thanks if anyone can help :smile:

-Blayke

There are several different packages which make this easy; see script and links
–Eric

edit: deprecated link removed.

You can use this too:

var startPosition : Vector3;
var endPosition : Vector3;
var speed = 10.0;

transform.position = Vector3.Lerp(startPosition, endPosition, speed * Time.deltaTime);
2 Likes

Move any object to any place at any speed (units/seconds) or in any time – all done without using Update:

StartCoroutine (MoveOverSeconds (gameObject, new Vector3 (0.0f, 10f, 0f), 5f));

public IEnumerator MoveOverSpeed (GameObject objectToMove, Vector3 end, float speed){
// speed should be 1 unit per second
while (objectToMove.transform.position != end)
{
objectToMove.transform.position = Vector3.MoveTowards(objectToMove.transform.position, end, speed * Time.deltaTime);
yield return new WaitForEndOfFrame ();
}
}

public IEnumerator MoveOverSeconds (GameObject objectToMove, Vector3 end, float seconds)
{
float elapsedTime = 0;
Vector3 startingPos = objectToMove.transform.position;
while (elapsedTime < seconds)
{
transform.position = Vector3.Lerp(startingPos, end, (elapsedTime / seconds));
elapsedTime += Time.deltaTime;
yield return new WaitForEndOfFrame();
}
transform.position = end;
}

7 Likes