Here’s my setup. I have a 4.6 UI Canvas with “Screen Space - Camera” render mode and its Render Camera set to Main Camera. I have a GameObject with Text component on it and I want to keep it aligned with a world space position, i.e. show a text overlay on a 3D map. I update the position of the object (the RectTransform of the GameObject) on Update():
Vector3 vpPosition = Camera.main.WorldToViewportPoint( m_position );
RectTransform wsRT = m_ui.GetComponent<RectTransform>();
Vector3 pos;
pos.x = wsRT.rect.width * vpPosition.x - wsRT.rect.width / 2f;
pos.y = wsRT.rect.height * vpPosition.y - wsRT.rect.height / 2f;
pos.z = 0f;
objectRT.localPosition = pos;
Here “m_ui” is the object with a Canvas set to “Screen Space - Camera” render mode. The “m_position” is the world position of the object.
The script works just fine, it correctly sets the position - but when I move the Main Camera from another script, there’s clearly a one frame lag? It’s like the new position calculated in Update() is based on camera position from the previous frame.