How do I stop camera movement from going out of frame while using lerp
I have been trying to make the camera move smoothly and now that it is being moved smoothly with lerp, I want to now restrict the camera from going out of bounds of the background.
I have tried using this:
public class CameraFollow : MonoBehaviour
{
public Transform player;
public float followSpeed = 1.5f;
private Vector3 destination;
private void Start()
{
destination = new Vector3(player.position.x + 21, transform.position.y, transform.position.z);
}
private void Update()
{
if (transform.position.x > -29.81 && transform.position.x < 30.7)
{
destination = new Vector3(player.position.x + 21, transform.position.y, transform.position.z);
transform.position = Vector3.Lerp(transform.position, destination, followSpeed * Time.deltaTime);
}
}
}
But the problem with this code is that after setting the condition false, the camera stops following the player as I have not described what to do after the camera is prevented from going out of the bounds and if the player stays within the bounds.
Note: In the above if statement, -29 and 30 are the x axes where the camera shows the background and just before it goes out of bounds of the background and the camera is in perspective view and I would like to keep it that way. Thank you in advance.
Your answer
Follow this Question
Related Questions
Camera not following properly with lerp 0 Answers
what wrong with lerp. 1 Answer
Simple Advice needed - Lerp, SmoothDamp, SmoothStep...... etc. 2 Answers
How to control rocket pathing with Quaternion.Slerp 1 Answer
GameObject Jittering 0 Answers