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.
GameObject
inside enemyGameObject
and rotate the canvas. But you can also avoid all of this by putting bothCanvas
andEnemy
under 1 parentGameObject
. Then you can move the parentGameObject
viaEnemy
and do the rotation of the enemy on enemyGameObject
. This way canvas will move with enemy but the rotation will be applied to enemy only. \$\endgroup\$