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?