Hello there.
I’m trying to achieve a very very simple ‘camera follow’ using linear interpolation, but there’s no way i can get it working properly. No matter what I do, when the game stutters, the camera goes crazy. I’ve discussed this extensively on IRC and we couldn’t find a proper solution, so I made an SSCCE and i’m attaching it to this post. Please note that you have to build in order to test because targetFrameRate doesn’t work on the editor.
I’ve tried smoothdamp and the same problem occurs.
Here is the only script of the sample (component attached to the camera):
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public Transform player;
public Vector3 cameraOffset;
public Vector3 speed;
public Transform staticReference;
public float cameraDelay;
void Update () {
player.position += speed*Time.deltaTime;
if (Vector3.Distance(player.position, staticReference.position) > speed.magnitude*1.01f){
staticReference.position = player.position+speed;
}
}
void FixedUpdate(){
//simulate stutter / fps drops
if (Random.Range(0,30) == 0){
Application.targetFrameRate = 5;
}
else{
Application.targetFrameRate = -1;
}
}
void LateUpdate() {
transform.position = Vector3.Lerp(transform.position,player.position+cameraOffset, 1.0f - (float)System.Math.Pow(cameraDelay, Time.deltaTime));
}
}
player is an ordinary sphere.
cameraOffset is 0,1,-2
speed is 0,0,30 (i know it’s fast)
staticReference is an object with two cubes for reference of speed
cameraDelay is 0.001 (because the speed is fast)
I’m using system.math.pow because cameraDelay used to be a double in the past to make sure the problem wasn’t float imprecision (forgot to change back).
I don’t see what’s wrong with that code, perhaps you guys could help me.
Thank you in advance.
PS: Unfortunately I can’t attach the build here, even using the most compact 7z compression. Unity devs: if you read this, please, consider increasing the file size limit to 10mb so we can at least attach SSCCEs which are generally 6-7mb compressed.
2277304–152826–CameraFollowTest.zip (122 KB)