Search Unity

UGUI UI lagging behind camera movement

Discussion in 'Cinemachine' started by MythicalCity, Apr 21, 2018.

  1. MythicalCity

    MythicalCity

    Joined:
    Feb 10, 2011
    Posts:
    420
    Hi I've recently added Cinemachine cameras to my game but I'm getting a big lag between the UI positions and the camera position. Ie: in this video the ui should be attached to the tanks but as I move the camera around the UI lags behind in a jelly look.

    I have my cinemachine brain set to LateUpdate (but I've tried Smart Update as well). The UI is a Screen Space - Overlay canvas.

     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,717
    This is a known issue in Edit mode, but if it's happening in Play mode then there's something wrong.
    Does this happen in Play mode as well as in Edit mode?
     
  3. MythicalCity

    MythicalCity

    Joined:
    Feb 10, 2011
    Posts:
    420
    Yes, it happens in play mode. The above gif is from play mode.
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,717
    Can you export me a small package that demonstrates this?
    Also: which versions Unity and CM?
     
  5. MythicalCity

    MythicalCity

    Joined:
    Feb 10, 2011
    Posts:
    420
    I'm using 2017.4.1f1 and cinemachine 2.1.10

    not sure if I can export some sort of package from the game, it would take quite a while to put something together and we are releasing next week.. But here's a script I use to anchor the UI to a gameobject, perhaps this cant be done on lateupdate with cinemachine?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AnchorUiToObject : MonoBehaviour {
    5.  
    6.     public Vector2 offset;
    7.     public Transform objectToFollow;
    8.     public bool followCurrentSelectedObject = false;
    9.  
    10.     private RectTransform uiElement;
    11.     private Camera mainCamera;
    12.  
    13.     // Use this for initialization
    14.     void Start ()
    15.     {
    16.         if (PlatformSettings.Instance.isVR)
    17.             mainCamera = GameObject.Find("Camera (eye)").GetComponent<Camera>();
    18.         else
    19.             mainCamera = GameObject.Find("MainCamera").GetComponent<Camera>();
    20.  
    21.         uiElement = GetComponent<RectTransform>();
    22.     }
    23.  
    24.     public void SetObjectToFollow(Transform followThis)
    25.     {
    26.         objectToFollow = followThis;
    27.     }
    28.  
    29.     void Update()
    30.     {
    31.         if (followCurrentSelectedObject) {
    32.             if (BattleManager.Instance.currentSelectedObject != null)
    33.             {
    34.                 objectToFollow = BattleManager.Instance.currentSelectedObject.transform;
    35.             }
    36.         }
    37.     }
    38.  
    39.     // Update is called once per frame
    40.     void LateUpdate ()
    41.     {
    42.         if (objectToFollow)
    43.         {
    44.             if (!PlatformSettings.Instance.isVR)
    45.                 uiElement.position = RectTransformUtility.WorldToScreenPoint (mainCamera, objectToFollow.position) + offset;
    46.         }
    47.     }
    48.  
    49. }
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,717
    ah! In that case your problem is most likely in Script Execution Order. Edit that and put
    AnchorUiToObject after CinemachineBrain.

    upload_2018-4-23_14-47-15.png
     
  7. MythicalCity

    MythicalCity

    Joined:
    Feb 10, 2011
    Posts:
    420
    Yes, that was it, thank you!
     
    Crokett, howong and Gregoryl like this.
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,717
    @abedathman8 was this post a mistake? It seems incomplete, and I don't really think it belongs in this thread...
     
  9. howong

    howong

    Joined:
    Oct 9, 2017
    Posts:
    6
    Oh my gosh, I was having troubles with making UI objects always face the camera and setting the lower priority (higher number) in Script Execution Order seemed to fix the issue even with or without Cinemachine. Thanks Gregoryl!
     
    frelind86 likes this.
  10. BrandStone

    BrandStone

    Joined:
    Jul 21, 2014
    Posts:
    79
    Hi @Gregoryl,
    I have the exact same issue as described by @capitalJmedia.

    My cinemachine brain is set to LateUpdate
    I have set the script execution order:
    upload_2019-2-19_8-59-49.png

    My script updates the UI element's position in LateUpdate:
    Code (CSharp):
    1. void LateUpdate()
    2. {
    3.     // Update marker position
    4.     Vector3 iconPosition = Camera.main.WorldToViewportPoint(markerPosition);
    5.     markerCtrl.rect.anchorMin = iconPosition;
    6.     markerCtrl.rect.anchorMax = iconPosition;
    7. }
    Unity version: 2018.3.5f
    Cinemachine version: 2.2.8
     
    frelind86 likes this.
  11. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,717
    The Brain emits a CameraUpdated event whenever it positions the camera. Could you hook into that and do your processing there?
     
    frelind86 likes this.
  12. BrandStone

    BrandStone

    Joined:
    Jul 21, 2014
    Posts:
    79
    Sweet! It works :)
    Thanks!

    I ended up using both LateUpdate and CameraUpdated because the tracked objects are also moving independently from camera movement.
     
    frelind86 and Gregoryl like this.
  13. electric_jesus

    electric_jesus

    Joined:
    Mar 8, 2015
    Posts:
    36
    Hi! Is there such event in a current version of Cinemachine? I couldn't find it.
     
  14. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,717
    CinemachineCore.CameraUpdatedEvent
     
  15. mitaywalle

    mitaywalle

    Joined:
    Jul 1, 2013
    Posts:
    250
    Hello, we use cinemachine and uGUI, update position in CinemachineCore.CameraUpdatedEvent callback didn't helped. Isn't reason in Canvas . screensSpace - Camera movement behaviour?
     
  16. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,717
    If you draw your GUI after the camera is updated, it should be stable. Cinemachine doesn't move the camera after that.
     
  17. mitaywalle

    mitaywalle

    Joined:
    Jul 1, 2013
    Posts:
    250
    Hello, thanks for fast answer! But I don't know when uGUI really draws, I only move it's positions on CameraUpdatedEvent callback
     
  18. TheVirtualMunk

    TheVirtualMunk

    Joined:
    Sep 6, 2019
    Posts:
    150
    Similar issue with AR rendering and solution here for anyone interested.