I seem to have misunderstood something about how LERP works. Im not sure what is missing. The object is rotating as it should, but it cant seem to get it to end. The transform.rotation dont seem to reach its target even if it has done so visually.
Quaternion rotationTarget;
Vector3 travelTarget;
float rotationLerpProgress;
float rotationLerpDuration = 1f;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (Input.GetMouseButtonDown (0)) {
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit rayHit;
if (Physics.Raycast (ray, out rayHit)) {
var rayHitPoint = rayHit.point;
travelTarget = rayHitPoint;
var rotationDirection = rayHitPoint - transform.position;
rotationDirection.y = 0;
rotationTarget = Quaternion.LookRotation (rotationDirection);
rotationLerpProgress = Time.deltaTime;
}
}
if (transform.rotation != rotationTarget) {
if (rotationLerpProgress < rotationLerpDuration) {
transform.rotation = Quaternion.Lerp (transform.rotation, rotationTarget, rotationLerpProgress / rotationLerpDuration);
rotationLerpProgress += Time.deltaTime;
print (string.Format ("({2}) - progress: {0} - duration: {1} ", rotationLerpProgress, rotationLerpDuration, rotationLerpProgress / rotationLerpDuration));
} else {
print (string.Format ("setting rotation ({0}) to target ({1})", transform.rotation, rotationTarget));
transform.rotation = rotationTarget;
}
} else {
print ("rotaton is at target! :)");
if (transform.position != travelTarget) {
rigidbody.AddForce (Vector3.forward);
}
}
}