- Home /
[SOLVED] Camera jitters while moving towards/away from player
Hello. I've been having this issue for basically the entire time I've been working on this project and I would REALLY like to solve this issue. I'm very new to the proper way to use cameras. So I tried this and it works, ok. (This code is for a third person platformer)
So I have it set so that the camera will always follow the player and translate forward based on the distance between them. capDistance is the set distance (float) that should be between the player and camera.
if (distanceBetweenPlayerAndCamera > capDistance && frontCollision == false) //If the camera is farther away from the player than the cap
{
for (int i = 0; i < adjustSpeed; i++) //Repeat a certain amount times and continually update distanceBetweenPlayerAndCamera
{
distanceBetweenPlayerAndCamera = Vector3.Distance (transform.position, new Vector3 (target.position.x, transform.position.y, target.position.z)); //Distance between camera and cube
float m = distanceBetweenPlayerAndCamera - capDistance; //Get the difference between the player and camera
transform.Translate (Vector3.forward * m * Time.deltaTime); //Move towards the player according to the difference
}
}
if (distanceBetweenPlayerAndCamera < capDistance && backCollision == false) //If the camera is closer to the player than the cap
{
for (int i = 0; i < adjustSpeed * 2; i++) //Repeat a certain amount times and continually update distanceBetweenPlayerAndCamera (times 2 to keep move faster than player)
{
distanceBetweenPlayerAndCamera = Vector3.Distance (transform.position, new Vector3 (target.position.x, transform.position.y, target.position.z)); //Distance between camera and cube
float m = capDistance - distanceBetweenPlayerAndCamera; //Get the difference between the player and camera
transform.Translate (Vector3.back * m * Time.deltaTime); //Move away from the player according to the difference
}
}
So this works pretty good. Except, when the player is either moving away or towards the camera, the camera will jitter slightly. It's not a super crazy jitter but it is noticable and VERY annoying! The only other contributing factor would be this code that adjusts the y of the camera in the same fashion:
if ((transform.position.y > yheight && downCollision == false) || (transform.position.y < yheight && topCollision == false))
{ //yheight is updated every 2 frames and is 3f higher than the player
for (int i = 0; i < adjustSpeed; i++)
{
float ytemp = yheight - transform.position.y; //Find the difference between the proper y height and the current y height for the camera
transform.Translate (Vector3.up * ytemp * Time.deltaTime); //Adjust y accordingly
}
}
Increasing the adjustspeed doesn't seem to do anything either :/ And when I decrease it, the camera moves too slow and the player and camera get separated by a great distance.
What is the strangest thing is there are a few, rare occasions when the camera won't jitter for a small bit on startup but will eventually go back to its old jitters before long.
Help?? New C# code would be appreciated! Thanks a TOOOON for anyone who helps me in advance!!
Since this sounds like a ti$$anonymous$$g issue, it would be super-helpful to know whether you were moving the player and camera in Update(), FixedUpdate(), or LateUpdate().
Answer by JustJunuh · Jul 05, 2015 at 05:01 AM
Alright so I switched all the players movement over to rigidbodies and changed the camera code to FixedUpdate() and everything is fixed!
Thank you all for your help.
Answer by AlwaysSunny · Jun 22, 2015 at 06:35 AM
Not for nothin, but this looks considerably more complicated than it needs to be. Also, it looks quite non-traditional, unless I'm just witnessing a lack of shortcut methods like Vector3.Lerp.
The best chase cams are really, really simple. At least until you get into stuff like moving it to avoid having geometry between the camera and its focused entity.
The first way to address camera jitter involves moving your camera in LateUpdate(){}. You can safely find the appropriate location for the camera during Update, but you should actually move it during LateUpdate.
If that doesn't improve the situation to your liking, also try visiting the script execution order page of the project settings, and make the camera script execute after other scripts.
If it still jitters noticeably, you might consider using an animation curve or other nonlinear method to dictate the distance the camera can move in a given frame according to its distance from its desired position. A standard linear interpolation is not always satisfactory.
The most important thing to realize is - and I struggled to comprehend this when I encountered camera jitter - there is nothing magical going on. It can be confusing, because the issue is tied to your perspective into the game world. Jitter is (usually, assuming we're using the word to mean the same thing) a manifestation of trying to move the camera without fully appreciating how it's being asked to move in a given frame. Any unsmoothed, significant, or otherwise unusual motion is going to add to the problem, and is often the root cause.
Thanks so much for the help! I moved all the chase related code (the first section) to LateUpdate() and it works great! I also changed the script order to my liking and that helped as well.
And also, I kinda don't know how to use .Lerp in this situation. Whenever I do, I end up worse then I was before.
So the movement seems to be seamless at times. However sometimes, when I play the game, there are extremely tiny camera jitters. I can't tell why. Sometimes it works, sometimes it doesn't. $$anonymous$$ainly I've noticed, it works the first time when you boot up Unity, but every time you run the game after the first play it gets slightly jittery. I can't tell why it does this.
Your answer
![](https://koobas.hobune.stream/wayback/20220613201243im_/https://answers.unity.com/themes/thub/images/avi.jpg)