- Home /
Why is an object's movement jerky, when the camera is also moving by following a different rigidbody?
I've seen a bunch of similar questions to this, but so far everything suggested hasn't helped me.
My set-up is as follows;
1) A capsule, that is moving with constant speed, using:
void Update()
{
transform.position += new Vector3(Time.deltaTime, 0, 0);
}
2) A sphere with a rigidbody, that is set to move on player input:
void FixedUpdate()
{
if (Input.GetKey(KeyCode.D))
{
GetComponent<Rigidbody>().AddForce(new Vector3(1, 0, 0));
}
}
3) A camera, set to follow the sphere (with some z offset, to keep everything in view):
void LateUpdate()
{
transform.position = Sphere.GetComponent<Rigidbody>().position + new Vector3(0, 0, -5);
}
With the capsule using Update, the sphere using FixedUpdate and the camera using LateUpdate (as above), the following gfy shows what happens:
https://gfycat.com/BreakableSilverAlabamamapturtle
As you can see, when the camera is moving, the capsule movement is really jerky.
Alternatively, if I move the capsule code into FixedUpdate, it becomes:
https://gfycat.com/RapidSleepyAbyssiniancat
Which is better, but still not perfect. In particular, the capsule movement appears jerky in the beginning, when the camera is stationary. (This isn't very noticeable in the gfy, but trust me, it's worse in real time!)
I've tried various other combinations of Update types, along with changing the interpolation of the rigidbody, all to no avail.
Is there any way to get the capsule moving smoothly all the time, regardless of camera motion? Or am I going to have to learn to live with it?
Answer by jrocamora · Oct 11, 2016 at 08:55 PM
With Update(), the problem is framerate, so isn't moving constantly according to the display perception.
In FixedUpdate(), the capsule is moving constantly, but you're still perceiving it as moving according to the display.
The problem is actually the camera following the sphere. AddForce doesn't do the same thing as just constantly updating the position like with the capsule. The movement that you are seeing in the capsule is both the camera and the sphere moving tiny bits because of the engine physics not moving the sphere at an explicitly constant speed, but since the camera is following the sphere, the sphere looks like it's the one that isn't moving.
Of course! I totally didn't realise the code moving the sphere would be the culprit.
Thanks for the help!
To be fair, I think the code moving the sphere is fine. As another answer suggests, it's the Rigidbody updating object position at fixed rate -- but camera updating its position at regular rate. The inconsistency between the two results in perceived jerkiness of rigidbody that is being followed.
Answer by Prosto_Lyubo · Nov 11, 2017 at 12:47 AM
I had similar issue. Turns out that apparently RigidBody
updates its position only in FixedUpdate (along with the physics and applied forces). In Inspector I set my rigid body's Interpolation to interpolate. The jagged movement turned into a pretty smooth one. To be sure there are no rapid changes in the object movement I also smoothed the force applied to the object (but that may not be desired behaviour - it's case dependant but worked very well for me):
private Vector3 lastAcceleration = Vector3.zero;
private const float damping = 0.7f;
[SerializeField]
private RigidBody2D rb;
private void FixedUpdate()
{
Vector3 acceleration = Vector3.zero;
acceleration = /* Calculate Your acceleration here */
lastAcceleration = Vector3.Lerp( acceleration, lastAcceleration, damping);
rb.AddForce( lastAcceleration, ForceMode2D.Force );
}
Answer by BorisOkunskiy · Apr 09, 2020 at 08:11 AM
As a general rule: if something needs to "follow" Rigidbody by updating transform.position
, such updates should happen in FixedUpdate
. Camera is not an exception, so changing LateUpdate
to FixedUpdate
in camera resolved all the jerkiness issues for me.
(Context: I had the same problem with trying to make camera follow the object with some smooth time via Vector3.SmoothDamp
. It appeared well when setting smooth time to 0, but any positive value resulted in player being rendered inconsistently/jerky/jagged.)
Your answer
Follow this Question
Related Questions
Very jerky camera movement with rigidbody 1 Answer
Camera Relative Movement With Rigidbody.AddForce 2 Answers
When camera switches, rigidbody.velocity doesn't work 1 Answer
Move RigidBody character relative to camera. 2 Answers
Movement feels like it's always going slightly off to the sides 1 Answer