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. MoveTowards and Lerp should both create a smoothly moving object, provided it's done in a loop of some sort (Update or Coroutine).

–

My 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.MoveTowards (startm, finm, > Time.deltaTime);

–

1 Answer

1

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);
}

Nope, cube didn't move at all after that.

–