Ive made a 2d sidescroller game with a camera following this tutorial:
however i noticed that the sprite my camera follows seems twitchy. I know this is not the case of physics or such, because checking the scene window shows it moving along smoothly. Any ideas?
Twitchy could be a sprite movement issue, or a camera movement issue. Hard to tell without an example of what you’re talking about and the relevant code you’re using.
What Joe said. Usually you can watch the game/camera object in the scene view during play mode to find the culprit.
That being said, camera jitter can often be a result of the order of transform operations. As an example, if the cameras position is based on the player, but the camera scripts update function is updating before the players position is updated in its script, then your 2 components are always going to be out of sync. Use script order execution in the editor preferences to fix the situation is the easiest fix.
could it be that the object’s movement is handled in Update() while the camera is in LateUpdate()?
edit: nope, that wasnt it
heres some of the camera code
void LateUpdate()
{
if(transform.position != Player.transform.position)
{
Vector3 camera = new Vector3(Player.transform.position.x, Player.transform.position.y, transform.position.z);
camera.x = Mathf.Clamp(camera.x, MinPosX, MaxPosX);
camera.y = Mathf.Clamp(camera.y, MinposY, MaxPosY);
transform.position = Vector3.Lerp(transform.position, camera, smoothing);
}
}
the player object is set to move right constantly via RB.velocity
if(test == 0)
{
anim.SetInteger("LeftOrRight", 0);
rb.velocity = new Vector2(1, rb.velocity.y);
}
else
{
anim.SetInteger("LeftOrRight", 1);
rb.velocity = new Vector2(-1, rb.velocity.y);
}
I’m not near my computer right now to provide any example code(typing code on a cell phone sucks).
I find your positioning code unusual
hopefully I can help after I get home.
Alternatively, have you heard of cinemachine? It’s a free camera system you can get through the package manager that pretty much handles about everything most games would want from a camera system.
You should check it out!
Thank you for letting me know about that, i’ll take a look at it, but i really would prefer to figure out whats wrong with mine instead.
its weird, because i made a game once before with this same kind of camera, and i had this same issue, and fixed it somehow. It’s been almost two years though, so ive forgotten. Argh!
You can check the script execution order.
Make sure hat your camera follow script is running after all scripts that modify the player / follow position.
As in, put the following behavior in LateUpdate()? Already done that, its been like that from the start.
heres a gif to show what im talking about, it might be hard to tell, but its especially obvious once the object reaches the right of the screen

edit: nevermind, this gif is so compressed it cant be seen well enough. ugh.
Where do you move your follow target?
In Update or LateUpdate?
If its LateUpdate it can be that the camera position is updated before the object is moved.
Via the script execution order you can make sure that the camera flow late update is called after all other late update calls.
i am late, but i got it to work. For some reason moving the camera from LateUpdate to FixedUpdate made it work fine. i am curious as to why, though.
ah yes. i forgot that if your object is moved in the physics update, then you want the camera to move in fixed update as well. Fixed update is called at a fixed time step and sometimes can get called twice in a frame, or sometimes skipped entirely in a frame. Meaning that the movement logic between the player and camera don’t always sync up. I think the general rule is if your using fixed update to move the player, you usually want to use fixed update to move the camera, and if you move your player in update, use late update to position the camera.