# Camera movement slows down

Camera follows player but slows down in the end when player reaches it’s target position faster?
I made it to dragg a bit, but when player stops, it starts to accelerate brake too slowly.

``````targetPos =
new Vector3(followTarget.transform.position.x, transform.position.y, followTarget.transform.position.z);
transform.position = Vector3.Lerp(transform.position, targetPos, (moveSpeed+faster) * Time.deltaTime);
``````

It is because of the TIme.deltaTime which slows it down

Firstly, give this a read so you understand how Lerp works: How to Lerp like a pro | Chico Unity3D

You’re not using it in a way that gives you much control. The first and second parameters are supposed to be, generally speaking, unchanging values, and the third parameter is a number 0-1 which represents the percentage between the two values to return.

So if you did “Mathf.Lerp(10, 20, 0.5f)” it would output 15, because it is 50% (0.5f) between 10 and 20.

So what you’ve got is a first parameter that is constantly changing, and a third parameter that represents a tiny percentage from transform.position to targetPos. So the closer it gets to targetPos, the less and less it moves each frame, because you’re using a tiny percentage of an ever-shrinking distance between transform.position and targetPos.

The parameters you’re using perhaps better fit MoveTowards?

``````Vector3 step = (moveSpeed+faster) * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, targetPos, step);
``````
1 Like

Thanks alot mate! I’ve been making a 2D Pixel Art game for 3 months, and now i’m in the point where everything’s pixel perfect instead of character movement, so when i move my character while camera is following, the character sprite is jittering slightly because pixel perfect style can’t handle sub-pixel positions, if i have understood correctly. Do you have any ideas on this problem? My sprites and every assets ppu is 64, I’m using pixel perfect camera and the camera solved the issue for the objects that are static, but every moving object “jitters” when the camera is moving.

I can make the character move pixel perfect, but when i round every floating point number to integer, it will jump 64 pixels on every move, but yes it contains the pixel perfectness. Is it even possible to make good looking pixel perfect movement with 64x64 sprite and PPU 64?

This problem makes me feel like i should’ve made my game PPU 1? But i’ve heard it eats lots of resources with big map and many sprites. There must be better way!

So, this is the problem in it’s all simplicity:

There is no stuttering effect if the sprite moves pixel per pixel, but now it moves in sub-pixel areas and i can’t get it working to move one pixel at a time

``````transform.localPosition = new Vector3(agentTransf.localPosition.x , transform.localPosition.y, agentTransf.localPosition.z);
``````

I have a 3D nav mesh under the 2D world, so that’s the reason of getting the positions from “agentTransf”, but i believe the problem is in this code, because if i change the code like this: (just changing the transform values to an integer.)

``````transform.localPosition = new Vector3((int)agentTransf.localPosition.x , transform.localPosition.y, (int)agentTransf.localPosition.z +zOffset);
``````

Then the player sprite hops/jumps 64 pixels on every move and is pixel perfect with the camera, and there’s no jittering at all. BUT the problem is how can i make it jump only 1 pixel on every movement, when the PPU is 64?

Or i don’t really know! I’ve been stuck on this for a long time.

You don’t need to move by integers to be pixel perfect. Since your pixels-per-unit is 64, that means each pixel is 1/64 units in size, (0.015625). So you can move in those increments.

So if you want to move 10 pixels to the right, you would move (10 * 0.015625) to the right.

Also check this out:

Unity has a 2D Pixel Perfect package that you can use as of 2018.2 or so.

1 Like

Thank you alot!! Now the sprites are moving perfeclty without stuttering effect. I corrected the Nav Mesh Agent movement like this:

``````double sum = (hit.point.x / 0.015625);
int sum2 = Mathf.RoundToInt((float)sum);
double sum3 = (double)sum2 * 0.015625;

double sumy = (hit.point.z / 0.015625);
int sum2y = Mathf.RoundToInt((float)sumy);
double sum3y = (double)sum2y * 0.015625;

Vector3 pooint = new Vector3((float)sum3, hit.point.y, (float)sum3y);

agentNavMesh.SetDestination(pooint);
``````
1 Like