ComplicatedEgg
Member
Hello! I would like some assistance with my scripting issues please! I've just started to code with C# and don't understand many aspects of it. I'm currently trying to implement an effect that happens when the player gets damaged.
For example, as the green foreground of the bar changes with the value of the Health attribute, there will be another bar at the background that is white.
When the player gets damaged, the white background will stay at the original health amount for a second, and then smoothly slide to the same length as the newly changed health amount. At this point the white background will not be seen until the player gets damaged again.
My problem:
When I get damaged, both the fill images in the canvas do not change in fill amount. So the health bar as a whole, essentially doesn't change in size/length. I think it is an issue with my code, especially at the part where it gets the Health attribute value and uses it to change the amount it fills the bar.
Here is my script:
Any help would be appreciated, thanks!
For example, as the green foreground of the bar changes with the value of the Health attribute, there will be another bar at the background that is white.
When the player gets damaged, the white background will stay at the original health amount for a second, and then smoothly slide to the same length as the newly changed health amount. At this point the white background will not be seen until the player gets damaged again.
My problem:
When I get damaged, both the fill images in the canvas do not change in fill amount. So the health bar as a whole, essentially doesn't change in size/length. I think it is an issue with my code, especially at the part where it gets the Health attribute value and uses it to change the amount it fills the bar.
Here is my script:
C#:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Opsive.UltimateCharacterController.Traits;
using Attribute = Opsive.UltimateCharacterController.Traits.Attribute;
public class HealthShrink : MonoBehaviour
{
private Attribute m_AttributeHealth;
private AttributeManager m_AttributeManager;
private const float DAMAGED_TIMER_MAX = 1f;
private Image barImage;
private Image damagedBarImage;
private float damageTimer;
public void Awake()
{
barImage = transform.Find("bar").GetComponent<Image>();
damagedBarImage = transform.Find("damagedBar").GetComponent<Image>();
m_AttributeManager = GetComponent<AttributeManager>();
m_AttributeHealth = m_AttributeManager.GetAttribute("Health");
}
private void Start()
{
SetHealth(m_AttributeHealth.Value);
damagedBarImage.fillAmount = barImage.fillAmount;
}
private void Update()
{
damageTimer -= Time.deltaTime;
if (damageTimer < 0)
{
if (barImage.fillAmount < damagedBarImage.fillAmount)
{
float shrinkSpeed = 1f;
damagedBarImage.fillAmount -= shrinkSpeed * Time.deltaTime;
}
}
}
private void OnUpdateValue(Attribute m_AttributeHealth)
{
damageTimer = DAMAGED_TIMER_MAX;
SetHealth(m_AttributeHealth.Value);
}
private void SetHealth(float healthNormal)
{
barImage.fillAmount = healthNormal / 100;
}
}
Any help would be appreciated, thanks!