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()
?