I’m at the point in the course where I am applying special abilities to my player. I’m struggling to make sure the special abilities are working correctly (energy consumption wise) because any time I take damage from an enemy it seems to deplete my energy bar as well as my health bar.
Here is the code that I am using for energy:
namespace RPG.Characters
{
public class Energy : MonoBehaviour
{
[SerializeField] RawImage energyBar;
[SerializeField] float maxEnergyPoints = 100f;
float currentEnergyPoints;
CameraRaycaster cameraRaycaster;
// Use this for initialization
void Start()
{
currentEnergyPoints = maxEnergyPoints;
}
public bool IsEnergyAvailable(float amount)
{
return amount <= currentEnergyPoints;
}
public void ConsumeEnergy(float amount)
{
float newEnergyPoints = currentEnergyPoints - amount;
currentEnergyPoints = Mathf.Clamp(newEnergyPoints, 0, maxEnergyPoints);
UpdateEnergyBar();
}
public void UpdateEnergyBar()
{
float xValue = -(EnergyAsPercent() / 2f) - 0.5f;
energyBar.uvRect = new Rect(xValue, 0f, 0.5f, 1f);
}
float EnergyAsPercent()
{
return currentEnergyPoints / maxEnergyPoints;
}
}
}
Here is a screenshot of the energy bar in the inspector. I’m using an entirely different bar over the health bar, so I’m not sure what might be causing the issue