Question by
ttiim · Sep 15, 2017 at 04:19 PM ·
camera-movementlerpmathf
Final position of the camera movement
Hello. I've got the following script that is supposed to pan the camera from the point A to the point B. Almost everything does work as it should, except for a one small detail regarding to the final position which is a point B.
The final position of the camera is not exactly the same as targetTo, so in case of returning the camera it will snap to the point B first..
Does anyone have a solution?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class camera_menu : MonoBehaviour {
public Transform targetFrom;
public Transform targetTo;
public Transform targetLookAt;
private float distance;
private float speed = 2.7f;
private float startTime;
private float distanceCovered;
private float distanceFraction;
void Start () {
transform.position = targetFrom.position;
distance = Vector3.Distance (targetFrom.position, targetTo.position);
startTime = Time.time;
}
void FixedUpdate () {
transform.LookAt (targetLookAt.position);
distanceCovered = (Time.time - startTime) * speed;
distanceFraction = distanceCovered / distance;
transform.position = Vector3.Lerp (targetFrom.position, targetTo.position, Mathf.SmoothStep (0, 1, distanceFraction));
if (distanceFraction > 1) {
GetComponent<camera_menu> ().enabled = false;
}
}
}
Comment