- Home /
The lower the framerate, the more the camera stuttters
Hello everyone! Today I opened a recording program called OBS and it lowered my FPS (understandably). However, I noticed that the camera stutters the lower the framerate (it even happens at 60 FPS, just less noticeably). The script is as follows:
public Transform target;
public float moveSpeed = 10f;
public float offset = -1f;
bool startFollowing = false;
// Update is called once per frame
void LateUpdate()
{
float targetYPos = target.position.y - offset;
Vector3 newPosition =
new Vector3(transform.position.x, targetYPos, transform.position.z);
//ignore this if statement
if (startFollowing)
{
//this makes the camera follow the player in only the Y axis
transform.position =
Vector3.Lerp(transform.position, newPosition, moveSpeed * Time.unscaledDeltaTime);
}
}
Would anyone be able to tell me what is wrong with my script? Thank you so much for your time.
Answer by Ermiq · Nov 18, 2019 at 01:11 PM
I suggest you to read this thread: https://forum.unity.com/threads/camera-stutters-in-a-simple-rotation.389899/
Read carefully what the user rorydriscoll
wrote there and try out his lerping method. It works awesome for following camera. Obviously you'll have to modify it to implement into your code because his code is for camera rotation actually but the essential element there is how you set the t
parameter in Lerp (source, target, t)
functions.
The problem with your code is the more delay between frames (Update() calls), the faster your camera have to move to reach the target position. And by multiplying deltaTime
by big numbers (like 10) you make it even more jittery.
With riridriscoll
's method you won't need such a big multipliers, it's opposite actually, you'll set less multipliers to make camera follow faster.
@Ermiq $$anonymous$$an... that thread was a journey. I ended up in his blog where I tuned a little bit of his (magical) solution into this:
public Transform target;
public float moveSpeed = 10f;
public float offset = -1f;
bool startFollowing = false;
// Update is called once per frame
void LateUpdate()
{
float targetYPos = target.position.y - offset;
Vector3 newPosition =
new Vector3(transform.position.x, targetYPos, transform.position.z);
if (startFollowing)
{
//this makes the camera follow the player in only the Y axis
float cameraY =
Damp(transform.position.y, newPosition.y, 0.0025f, Time.deltaTime);
transform.position = new Vector3(transform.position.x, cameraY, transform.position.z);
}
}
// Smoothing rate dictates the proportion of source remaining after one second
public static float Damp(float source, float target, float smoothing, float dt)
{
return $$anonymous$$athf.Lerp(source, target, 1 - $$anonymous$$athf.Pow(smoothing, dt));
}
That worked perfectly. Thank you so much for setting me on the right path!
Your answer
Follow this Question
Related Questions
Framerate Dependent Mesh Deformation 1 Answer
Accurate Frames Per Second Count 5 Answers
How do I find the frames per second of my game? 6 Answers
How to change framerate for Android? 1 Answer
Parenting Weapon to Empty = Framerate drops to 2fps 2 Answers