- Home /
How do I move a Cube transform from one position to another smoothly?
I am working on a simple game. Right now I am working on moving a cube from one position to another. I am randomly generating a position and I want the cube to move there smoothly. Over time, I want it to move faster.
I have looked into Vector3.MoveTowards and Vector3.Lerp but it still not working. The cube still jumps around a lot and does not gradually move to the new position.
Please post your code so we can help you correct it. $$anonymous$$oveTowards and Lerp should both create a smoothly moving object, provided it's done in a loop of some sort (Update or Coroutine).
$$anonymous$$y code is:
Vector3 startm = new Vector3(px - 0.5f, 0.5f, py - 0.5f); Vector3 finm = new Vector3(cx - 0.5f, 0.5f, cy - 0.5f); Player.transform.position = Vector3.$$anonymous$$oveTowards (startm, finm, Time.deltaTime);
Answer by iwaldrop · Apr 17, 2013 at 02:17 AM
Just taking a wild guess here, but try this:
public float speed = 2;
Vector3 startPos = Vector3.zero;
Vector3 endPos = Vector3.up * 35;
void Awake()
{
transform.position = startPos;
}
void Update()
{
Vector3.MoveTowards(transform.position, endPos, Time.deltaTime * speed);
}