Hi I have an energy bar GUI element that decresases from 100 and this impacts the height of a mask to reveal an animated sprite. The mask height can be increased based on an arduino sensor imput but I am trying to limit the energy value to 100 using this line of code.
Mathf.Clamp(energy,0f,100f);
Can anyone explain what is wrong ?
public class Timer : MonoBehaviour
{
public static float energy = 100.0f;
public void Update()
{
Mathf.Clamp(energy,0f,100f);
if (JoeysInput.flex <= 0)
{
energy -= 10 * (Time.deltaTime);
}
if (JoeysInput.flex >= 2) {
energy += 6;
}
Hi - thanks for the feedback. I changed the script around to this:
rTrans.sizeDelta = new Vector2(200, (Mathf.Clamp(energy, 0, 200)));
Although the ‘energy’ float value doesn’t increase beyond the clamped maximum threshold…when it does reach the max value of 200 it no longer decreases based on the line:
energy -= 10 * (Time.deltaTime);
Any ideas ?
public static float energy = 200.0f;
public void Update()
{
if (JoeysInput.flex <= 0) {
energy -= 10 * (Time.deltaTime);
}
if (JoeysInput.flex >= 2) {
energy += 6;
}
if (JoeysInput.flex >= 4) {
energy += 10;
}
if (JoeysInput.flex >= 6) {
energy += 14;
}
if (JoeysInput.flex >= 8) {
energy += 18;
}
if (JoeysInput.flex >= 10) {
energy += 22;
}
RectTransform rTrans = (RectTransform) transform.GetComponent();
rTrans.sizeDelta = new Vector2(200, (Mathf.Clamp(energy, 0, 200)));