"Clipping", "Jumping" UI slider, attached to the 2d game object ( health bar)
My ui-slider jumps for no reason, i really don't understand what's happening. I have tried to change void Update() to FixedUpdate, but result same.
my code:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HealthBarUi : MonoBehaviour
{
public Slider Slider;
public Color Low;
public Color High;
public Vector3 Offset;
public EnemyHealth healthofenemy;
public void SetHealth(float health, float maxHealth)
{
Slider.gameObject.SetActive(health < maxHealth);
Slider.value = health;
Slider.maxValue = maxHealth;
Slider.fillRect.GetComponentInChildren<Image>().color = Color.Lerp(Low, High, Slider.normalizedValue);
}
void Update()
{
SetHealth(healthofenemy.currentHP, healthofenemy.maxHP);
Slider.transform.position = Camera.main.WorldToScreenPoint(transform.parent.position + Offset);
if (healthofenemy.currentHP <= 0f)
{
gameObject.SetActive(false);
}
}
}
Comment