How to make a transform.Translate () smoothed in unity5?

1

I need to translate a GameObject to any destination, but the transition must be smooth at a constant rate in such a way as to last the time that is passed by parameter. For example:

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

This function should translate obj to destination in an exact time of durationInMs milliseconds. I tried doing this using steps rather than tepo, but the precision gets very small on 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 Move() function?

    
asked by anonymous 04.09.2016 / 10:24

2 answers

1

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

    
04.09.2016 / 19:34
0

I do not quite understand if this is what you want, but read on:

link

or

link

    
19.06.2017 / 19:42