- Home /
Update/Late Update jitter problem(non FixedUpdate related)
Hey guys i am experiencing an issue where an object placed on the scene follows the main camera and jitters if the camera moves in the LateUpdate() method. The way i understood is late update gets called once per frame after Update() is finished, so there should be really no problem moving the object like that. The way i created the scene:
Create a new empty scene.
Set the camera to orthographic and attach this script to it:
public class CameraMoverTest : MonoBehaviour { private void Update() { transform.position += new Vector3(1, 0, 0) * Time.deltaTime * 20; Debug.Log("Camera Position X: " + transform.position.x); } //// Jitters //private void LateUpdate() //{ // transform.position += new Vector3(1, 0, 0) * Time.deltaTime * 20; // Debug.Log("Camera Position X: " + transform.position.x); //} }
3.Create a new game object, add SpriteRenderer component and add some sprite to visualize the jitter and add this script to the same game object:
public class MovedObjectTest : MonoBehaviour { private Vector2 m_initialPosition; private void Start() { m_initialPosition = Camera.main.transform.position; } private void Update() { Vector3 position = Camera.main.transform.position; position.z = 0; transform.position = position; Debug.Log("Object Position X: " + transform.position.x); } }
So the object does not jitter if the camera moves in the Update() method, but if you uncomment the LateUpdate() method and comment the Update() you will see a noticeable jittery movement over the X axis of the moving object. Both debug logs shows exactly the same camera and object positions. I can't really see where the problem is. What am i doing wrong? Please explain.
EDIT: So i did some test and came up with some observations. It seems its something to do with the order of execution.
Observation 1
Moved Object - Updating Method : LateUpdate()
Camera - Updating Method : LateUpdate()
Result: Jitter
Observation 2
Moved Object - Updating Method : LateUpdate()
Camera - Updating Method : Update()
Result: NO Jitter
Observation 3
Moved Object - Updating Method : Update()
Camera - Updating Method : Update()
Result: Jitter
Observation 4
Moved Object - Updating Method : Update()
Camera - Updating Method : LateUpdate()
Result: NO Jitter
Answer by Nanity · Mar 29, 2015 at 08:46 AM
Option #4 is the correct execution. See http://docs.unity3d.com/ScriptReference/MonoBehaviour.LateUpdate.html
In Options #1 and #3 you need to take care of the script execution order. Generally a bad advice to rely on this. http://docs.unity3d.com/Manual/class-ScriptExecution.html
Option #2 basically is the same as #4, but either the camera or the object is behind one frame regarding the movement.
Your answer
Follow this Question
Related Questions
Stacked cameras not working after update 2018.3 -> 2019.2 0 Answers
How can I get my camera to momentarily pause between different positions when using lerp? 0 Answers
Is my (LateUpdate) camera + rigidbody causing jitter? 2 Answers
How would you render a seemlessly wrapping scene? 1 Answer
Unity - Render XR game objects onto processed camera image 0 Answers