I have a 3D plane with "enemies", each one has it's canvas with a health bar, that follows the enemy position. When the enemy rotates, the health bar position gets laggy and jittery (you see no fluently, like a jump)

The code that I'm using is:

 private Camera _camera;
 private RectTransform _healthBarReact;

 void Start {
     _camera = GameObject.Find("Camera").GetComponent<Camera>();
     _healthBarReact = transform.FindChild("HealthBarImage").GetComponent<RectTransform>();
 }

 void Update {
       Vector3 screenPoint = _camera.WorldToScreenPoint(transform.parent.position); // the parent is the enemy, I attached the script to the child canvas "HealthBar"
     _healthBarReact.position = screenPoint;
 }

How can I fix that "lag" issue? apparently, the issue only occurs when the enemy is rotating... when it follows you in a straight line, the healthbar looks well.

  • 1
    couldn't you just stick the canvas to the enemy gameobject and set its rotation to identity on the LateUpdate()? – Leggy7 Aug 27 '16 at 8:50
  • As @Leggy7 mentioned - just put the canvas GameObject inside enemy GameObject and rotate the canvas. But you can also avoid all of this by putting both Canvas and Enemy under 1 parent GameObject. Then you can move the parent GameObject via Enemy and do the rotation of the enemy on enemy GameObject. This way canvas will move with enemy but the rotation will be applied to enemy only. – Candid Moon May 6 at 12:22

All UI follow functionality should be implemented in LateUpdate() method. See manual.

Example using your code:

void LateUpdate {
    Vector3 screenPoint = _camera.WorldToScreenPoint(transform.parent.position); // the parent is the enemy, I attached the script to the child canvas "HealthBar"
    _healthBarReact.position = screenPoint;
}
  • 2
    To give a bit more context for why this change is useful: LateUpdate runs after all the other Update scripts, and any animations that might change the positions/orientations of things in the world. So it helps avoid lag problems where your following object is constantly chasing where its lead object was instead of tracking where it is in the latest frame. – DMGregory Aug 27 '16 at 11:29

Camera matrix update is delayed until very late in frame (probably updates just before rendering). WorldToScreenPoint relies on matrix to calculate screen position.

To make it work correctly you have to do two things:

  1. Ensure HealthBar update is called AFTER input handling (which moves camera)
  2. Force Camera matrix to be updated

The code:

using UnityEngine;

public class HealthBarPositionUpdate : MonoBehaviour
{
    public GameObject Enemy;
    public RectTransform HealthBarElement;

    // LateUpdate ensures that camera position was already updated
    private void LateUpdate()
    {
        Camera.main.ResetWorldToCameraMatrix(); // Force camera matrix to be updated
        var targetPos = Camera.main.WorldToScreenPoint(Enemy.transform.position);
        if (targetPos.z > 0)
        {
            var pos = targetPos.Round(); // Pixel snapping
            pos.z = 0;

            HealthBarElement.SetPositionAndRotation(pos, Quaternion.identity);
        }
    }
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.