SmoothDamp and rounding problem
Hey,
I'm using Vector3.SmoothDamp to make my camera follow my player smoothly, it works perfectly, but i'm trying to round my camera's position so that it snaps to the pixels.
My code works fine in editor but when running the build with a much higher FPS, the cam is super jerky and fast.
There must be something i'm doing wrong, is anyone able to explain what is going on?
This is running in Update, why is it framerate dependent?
Vector3 smoothedCam = Vector3.SmoothDamp(cam.transform.position, player.transform.position, ref velocity, camSpeed);
cam.transform.position = new Vector3(Mathf.Round(smoothedCam.x * pixelsPerUnits) / pixelsPerUnits, Mathf.Round(smoothedCam.y * pixelsPerUnits) / pixelsPerUnits, cam.transform.position.z);
Interesting, so what sort of "snap" behavior are you looking for? To end in an integer coordinate, or to move with integer coordinates entirely?
I want my camera to move pixels by pixels so that my game stays pixel perfect, with a Pixels Per Unit of 100 this would mean rounding the position to every 0.01 in world units.
But i still want the smooth behavior that SmoothDamp gives me.
I don't understand why rounding the smoothed out vector gives me different results at different framerates.
Hmm, could it be because the faster $$anonymous$$athf.Round is updating, the more "skips" i get to the nearest 0.01?
Hm, the documentation mentions that the referenced velocity value you supply will be modified with each call. Since the rounding you've set up should already be doing the "only move when a new pixel is available" behavior, this might be causing problems?
Is that being deter$$anonymous$$ed before this block is reached in Update, or is this function the only thing controlling it?
The same issue occurs with Lerp multiplied by deltatime so i'm not sure about SmoothDamp's velocity, the code i pasted is pretty much the only thing happening in the script other than setting my variables. I assumed that rounding the cam's position would have the same behavior than using SmoothDamp, but having it jump by 0.01 ins$$anonymous$$d, i don't understand the framerate affecting the speed only when rounding, it has to be the speed at which the rounding is updating making the cam move faster, my brain isn't cooperating today.
Answer by Khena_B · Jan 23, 2017 at 10:05 PM
Solved it by using SmoothDamp on a new Vector3, and instead made the camera follow this vector, then rounded the camera's position.
Answer by Juandapp · Nov 21, 2019 at 05:44 AM
Hi, did you solve it? I have a similar problem with the camera and my FPS
Answer by Siliko · May 21 at 05:16 PM
Turned nice rounding camera after smooth damp, help me a bit but not fixed yet