I’m making a side scrolling game with a car, and want the camera to zoom out as it speeds up so the player can see what’s ahead. I’m not sure how I would use lerp inside the update function to get a smooth zoom. Using lerp inside the update function is slightly smoother, but still very jagged.
Is your car moved with physics or in FixedUpdate? If so, then smooth camera movement will only be possible if the camera is also moved in FixedUpdate.
If that doesn’t resolve it, post your current code so we can see the issue.
Car is moved with physics. The first part is moving the camera with the player car. The second part is so it keeps a minimum camera size so it doesn’t zoom in too much.
using UnityEngine;
using System.Collections;
public class follower : MonoBehaviour {
public GameObject player; //Public variable to store a reference to the player game object
float velocity;
float nextcamsize;
float lastcamsize;
public float zoomspeed;
private Vector3 offset; //Private variable to store the offset distance between the player and camera
// Use this for initialization
void Start ()
{
nextcamsize = 5;
lastcamsize = 5;
//Calculate and store the offset value by getting the distance between the player's position and camera's position.
offset = transform.position - player.transform.position;
}
// LateUpdate is called after Update each frame
void LateUpdate ()
{
// Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance.
transform.position = player.transform.position + offset;
//Camera.main.fieldOfView = add zoom functionalit
}
void Update(){
velocity = player.GetComponent<Rigidbody2D>().velocity.x;
lastcamsize = nextcamsize;
if (velocity < 5) {
nextcamsize = 5;
} else {
nextcamsize = velocity/2;
if(nextcamsize<6){
nextcamsize = 5;
}
}
Camera.main.orthographicSize = Mathf.Lerp (lastcamsize,nextcamsize,Time.deltaTime * zoomspeed);
//Camera.main.orthographicSize = Mathf.SmoothDamp(lastcamsize,nextcamsize);
}
}