How to make a Transform. Smoothed translate() in unity5?

Asked

Viewed 317 times

1

I need to move one GameObject to any destination, but the transition must be smooth at a constant speed such that it lasts the time that is passed by parameter. For example:

void Move(GameObject obj, Vector3 destination, float durationInMs) {}

This function should translate obj for destination in an exact time of durationInMs milliseconds. I tried to do this using steps instead of toe, but the accuracy gets very small in machines with high FPS rate:


using UnityEngine;
using System.Collections;

public class SquareMovementBehaviour : MonoBehaviour {
    Vector3 initialPosition;
    Vector3 finalPosition;
    Vector3 currentPosition;
    Vector3 step;

    // Use this for initialization
    void Start () {
        initialPosition = transform.position;
        finalPosition = new Vector3(10, 0, 0);
        step = finalPosition - initialPosition;
    }

    // Update is called once per frame
    void Update () {
        if (Vector3.Distance(transform.position, finalPosition) >= 0.1) // Precisão fraca. Não funciona :/
            transform.Translate (step * Time.deltaTime);
        else
            transform.position = finalPosition;
    }
}

How do I implement this function Move()?

2 answers

1


Although your question is quite different of your other question (and therefore not duplicated), the answer is essentially the same answer I gave there. There is just one more detail, which is that you should add some more method to define or reset the value of the fields destino and velocity.

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.