- Home /
Camera field of view issues with lerp, DoTween, iTween etc.
Hi all,
I'm having an issue with smoothly moving the camera field of view in my game that I cannot resolve.
I have a script attached to the camera that follows the player, this checks if the player is within a certain distance to an object, if it is then the field of view zooms in, if it is not within a certain distance then the field of view zooms out.
In my code I have a list of objects and I iterate over each one to check the distance. The code does work, but the more objects I add to the list the less the field of view zooms in or out. I tried math.lerp, DoTween and iTween and I get the same issue with all of them so the issue is with how I've coded it.
Thanks in advance!
Here's my cameraFollow script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class CameraFollow : MonoBehaviour
{
public GameObject player;
private GameObject launchPad;
private GameObject tnt;
private Camera cam;
public float cameraHeight = 20f;
public float offsetX = 0;
public Vector3 pos;
private float targetFOV = 25.0f;
private float defaultFOV = 40.0f;
//public float speed = 0.2f;
public float distanceToBject = 10.0f;
[SerializeField] float cameraZoomSpeed = 0.5f;
List<GameObject> fovGroup = new List<GameObject>();
// Use this for initialization
void Start()
{
launchPad = GameObject.Find("Launchpad2");
tnt = GameObject.Find("tnt_low");
DOTween.SetTweensCapacity(2000, 50);
cam = Camera.main;
fovGroup.Add(launchPad);
fovGroup.Add(tnt);
}
// Update is called once per frame
void LateUpdate()
{
pos = player.transform.position;
pos.x += offsetX;
pos.z += cameraHeight;
transform.position = pos;
ZoomFOV();
}
private void ZoomFOV()
{
foreach (GameObject pickup in fovGroup)
{
if (pickup == null)
{
return;
}
if (Vector3.Distance(pickup.transform.position, player.transform.position) <= distanceToBject)
{
ZoomCameraIn();
}
else
{
ZoomCameraOut();
}
}
}
private void ZoomCameraIn()
{
cam.DOFieldOfView(targetFOV, cameraZoomSpeed).SetEase(Ease.OutSine);
}
private void ZoomCameraOut()
{
cam.DOFieldOfView(defaultFOV, cameraZoomSpeed).SetEase(Ease.OutSine);
}
}
Answer by big_stoater · Jul 01, 2018 at 06:53 AM
got it sorted, didn't realise that the logic in the code was fighting with itself, School boy error!
This was a long time ago but can you elaborate on how you fixed it? I'm finding when I try zoom in with focalLength and Lerp/DoTween it stutters, or doesn't reach the required focalLength
Hi,
The issue I had was that it worked with a single object. When I added more than one the logic in the code didn't work as it was trying to zoom in/out on all the objects simultaneously. I think I fixed this by using colliders on the objects ins$$anonymous$$d.
I also had issues with lerp. The camera would never go the the FOV fully. I changed to using DoTween ins$$anonymous$$d worked as expected.
hope that helps!