Script:
using UnityEngine;
public class AimCamera : MonoBehaviour
{
public Camera cam;
public Transform start;
public Transform end;
public float smoothSpeed;
private float startTime;
private float journeyLength;
//private Vector3 resetPos;
void Start()
{
startTime = Time.time;
journeyLength = Vector3.Distance(start.position, end.position);
}
//TODO FIX THIS
void LateUpdate()
{
if (Input.GetButton("Fire2"))
{
float distCovered = (Time.time - startTime) * smoothSpeed;
//float between 0 and 1
float fractionOfJourney = distCovered / journeyLength;
cam.transform.position = Vector3.Lerp(start.position, end.position, fractionOfJourney);
}
else if (Input.GetButton("Fire2") == false)
{
float distCovered = (Time.time - startTime) * smoothSpeed;
float fractionOfJourney = distCovered / journeyLength;
cam.transform.position = Vector3.Lerp(end.position, start.position, fractionOfJourney);
}
}
}
I have empties as a child of the player, likewise the camera is in the same “position” in the hierarchy.
The idea is to lerp the camera from point A (start) far away, to point B (closer to the player) when pressing Fire2. Think of fortnite’s aiming system. There are several problems:
- The lerp seems to happen at all times even if Fire2 is not held, from A to B. Fire2 instead acts as a “switch camera” mode without any smoothness, which is not what i want.
- The lerp from B back to A does not work. After camera lerp from A to B, the camera stays at B. I tried to fix this in the “else if” part, but to no avail.
Help is appreciated!