Search Unity

UI element in world space has delayed movement

Discussion in 'UGUI & TextMesh Pro' started by Darkkingdom, Mar 23, 2019.

  1. Darkkingdom

    Darkkingdom

    Joined:
    Sep 2, 2014
    Posts:
    81
    Hey guys,

    I'm currently on a healthbar system which is a UI Image which follows the enemys in world space.
    It works but the image has always a slight delay when the camera moves, which is pretty annoying.
    I set the image position like this:

    Code (CSharp):
    1. Vector2 enemyScreenPosition = Camera.main.WorldToScreenPoint(enemyPosition);
    2. RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform, enemyScreenPosition, Camera.main, out canvasPosition);
    3. image.transform.localPosition = canvasPosition;
    My camera is moved in LateUpdate and is Orthographic.
    The canvas is set up like this:


    I already tried moving the UI image in LateUpdate and FixedUpdate but nothing changed.
    Also, I tried switching the ScriptExutionOrder suggested in this:
    https://forum.unity.com/threads/ugui-ui-lagging-behind-camera-movement.527902/
    But sadly it didn't help either :(


    Thank you very much :)
     
    h0neyfr0g likes this.
  2. Darkkingdom

    Darkkingdom

    Joined:
    Sep 2, 2014
    Posts:
    81
    Has anyone an idea?
    If any more information is needed I'm more than happy to oblige :)
     
  3. Darkkingdom

    Darkkingdom

    Joined:
    Sep 2, 2014
    Posts:
    81
    *bump*
    Is there no way to fix this?^^
     
  4. h0neyfr0g

    h0neyfr0g

    Joined:
    Jul 13, 2019
    Posts:
    35
    I am also having this problem
     
  5. t-yoshino

    t-yoshino

    Joined:
    Oct 6, 2015
    Posts:
    1
    maybe :D
    [code = CSharp] private void LateUpdate(){

    Vector3 world_pos = _world_object.transform.position;
    var screen_pos = RectTransformUtility.WorldToScreenPoint(_world_camera、world_pos);

    // -0.5から0.5に正規化します
    Vector2 normalized_pos_from_center;
    Normalized_pos_from_center.x = screen_pos.x / Screen.width-0.5f;
    Normalized_pos_from_center.y = screen_pos.y / Screen.height-0.5f;

    //この場合、中心からのみ固定
    Vector2 anchored_pos = normalized_pos_from_center * _canvas.sizeDelta;
    _transform.anchoredPosition = anchored_pos;
    }[/コード]
     
    Last edited: Mar 16, 2020