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