This works perfectly fine and smooth. However the camera is very rigid, I would like to add some delay/easing effect. If I apply Lerp the camera movement becomes jittery immediately:
It’s worth mentioning this is not the same kind of jitter that you get when you have RigidBody movements and you update the camera in Update(). I also have that (since player X/Z movements are done by RigidBody#AddForce(). That I can fix if I move the Camera update from LateUpdate() to FixedUpdate(). However the other jitter caused by Lerp+rotation is not fixed even there.
What I’m mainly looking for are guidelines on how to handle a camera following a player object:
if there is at least one movement on a RigidBody (eg AddForce) should all related (eg Camera) updates be done in FixedUpate()?
putting some updates (eg Camera movements) to FixedUpdate() looks visibly less responsive (compare to default game frame rate)
is Lerp a bad option for delayed movements for a camera?
Ah, thank you for pointing to SmoothDamp, didn’t know that. However it hasn’t changed much. Cinamechine I’ve heard, however in this case I really want to understand how should this be done. I feel it shouldn’t be that hard. As well I would like to learn when to use Update/FixedUpdate/LateUpdate in respect of transform vs rigidbody movements.
Thanks for the tip. Indeed LookUp() seems simpler, though if I see the signature variations correctly it does not allow an offset to be used. In my case I need the player to be a bit on the lower half of the camera view, not in the middle.
Unfortunately the jittering effect is still there when using Time.deltaTime. Same if I use fixedDeltaTime in FixedUpdate. Gotta be something else. Thanks anyways!
Camera stuff is honestly more complicated than you’d expect. It can quickly become a deep rabbit hole.
You are probably right on that Haven’t even thought about camera colliding with objects …
FixedUpdate for anything physics, LateUpdate for camera and anything else that is timing sensitive, and Update for everything else.
Thanks for confirming that. That’s roughly what I distilled. Though it still contradicts what I observe. If I put camera movements to LateUpdate or Update - all the jitter is much worse. FixedUpdate at least is not jittering on movements, only on rotation. Though I’m sure it’s something on my side that I’m missing.
Usually you need an extra buffer between the camera and the player. Ergo an, object that follows the player, and the camera follows said object. I do this even with cinemachine.
But seriously, just use cinemachine. You’ll save yourself weeks of headache. Literal. Weeks.
I did some debugging. When a RigidBody is falling with gravity applied, the update of position is done by the physics engine at their rate, which is not the render frame rate. So printing the Y position of the falling object in Update() gives:
Notice here the gaps of updates. So here when a Lerp or SmoothDamp is applied in an Update/LateUpdate it will interpolate in those gaps where the physics engine is not running. It gets to a point where the physics engine is called, which introduces a larger jump for the Lerp/SmoothDamp to work with. So the jitter is these occasional larger distances caused by the physics engine’s lower call rate.
Not sure how would I make that interpolation result in equal deltas when I don’t know when the physics engine cycle is kicking in.
Yes, that indeed did the trick. I think the issue was that if I put the camera follow Lerp into Update than the jitter comes from the rigidbody updating less frequent and if I put it into FixedUpdate then the jitter comes from the transform (non rigidbody movement) progressing more than FixedUpdate itself. All in all - when there is a dependent delayed movement, the origin is kinda have to do either a transform based update or a rigidbody update but not both. And since now I do movement + rotation both with rigidbody, using camera follow in FixedUpdate with Lerp makes it smooth (since nothing moves between iterations).
Well even with a static camera if you rotate a rigidbody from within Update by directly modifying its transform while the rigidbody is moving then the rigidbody will jitter. In general we’re not supposed to modify rigidbodies through their transforms.
You can get away with rotating a rigidbody in Update if you use MoveRotation. For example you may feel that AddTorque isn’t responsive enough and so you could rotate in Update with this: