- Home /
Camera Lerp from A to B, and back
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!
Answer by xxmariofer · Oct 16, 2019 at 12:49 PM
void Update()
{
float amount = smoothSpeed * Time.deltaTime;
fractionOfJourney += (Input.GetKey("Fire2")) ? amount : -amount;
fractionOfJourney = Mathf.Clamp(fractionOfJourney, 0, 1);
cam.transform.position = Vector3.Lerp(start.position, end.position, fractionOfJourney);
}
this code should work
Now i am one step closer. the lerp is now working, but at the point of initializing the camera pos is at the end position without pressing fire2. Also, after the "lerp" that goes on continously without pressing fire2 eventually finishes, meaning that pressing fire2 after a given time just teleports the camera back and forth on input, without the lerp. Thanks for the help!
Set fractionofjourney yo 0 ya te start. Smooth speed Is a constant? The code was untested but seems fine to me, ill test it tommorrow
Answer by misher · Oct 18, 2019 at 07:50 AM
public class CameraTransition : MonoBehaviour
{
[SerializeField] Transform camTransform;
[SerializeField] Transform head;
[SerializeField] Transform arm;
Transform current = null;
private void Start()
{
current = head;
}
void Update()
{
current = Input.GetButton("Fire2") ? arm : head;
camTransform.position = Vector3.MoveTowards(camTransform.position, current.position, 0.1f);
camTransform.rotation = Quaternion.RotateTowards(camTransform.rotation, current.rotation, 1f);
}
}
Make sure to chose the right amount for rotation step and translation step to be sure your rotation and translation are both finished at the same time, I believe you can calculate it or you can play with some values.