I am trying to move GameObjects to different places on the screen, I want them to move like an animation, smoothly moving over while the programming is running. I am using Unity with Leapmotion

I have tried the code below but this just moves the object into the new position immediately, it doesn't move it slowly so that the user can see it move from current location to new. - like an animation.


Your help will be appriciated. Thankyou in advance

3 Answers

Bizhan On

You must either use Time.deltaTime in Update or Time.fixedDeltaTime in FixedUpdate


myobject.transform.position += (direction * Time.deltaTime);
akaBase On

transform.Translate will continue moving an object in the direction provided

What you are looking for is Vector3.Lerp

obj.transform.position = Vector3.Lerp(obj.transform.position, new Vector3(0f, 0.5f, 0f), Time.deltaTime * speed);
rabbit on On

One more way to move GameObject, but instead of specifying the destination point, the motion vector is used:

private Rigidbody rb;

void Start()
    rb = GetComponent<Rigidbody>();
    ChangeDirection(new Vector3(1,1,0));

void ChangeDirection(Vector3 dir)
    rb.velocity = dir;

Don't forget to add a Rigidbody component on object.