C# float variable isnt behaving like its value
I have this set up so that my character has an energy bar that drains over time. I'm trying to set it so that if it's below 50% it drains faster, then same with below 30% and 20% with < 20% draining very quickly. about .008% per frame, I think. (the normal drain rate is about .0004% per frame) if I watch the energyDrainSpeed in the inspector while running my game, I can see it increasing but it doesn't behave the same as the number by itself does. what I mean is... if I change the DrainEnergy function so that there's just 500.0f instead of energyDrainSpeed, the character's energy drains a lot faster than it does when energyDrainSpeed is equal to 500.0f (or higher) I also tried setting the energyDrainSpeed to over 12000.0f but it definitely wasn't draining at a speed of 1% per frame. then when I replaced the variable with just 12000 in the script - like curEnergy - (12000 / 12000) - it did drain at a rate of 1% per frame.
Am I just getting lag or something?
this is the code I'm working on;
using UnityEngine;
using System.Collections;
public class PlayerEnergy : MonoBehaviour
{
public float maxEnergy = 100;
public float curEnergy = 100;
public float energyDrainSpeed = 5.0f;
public float energyBarLength;
void Start()
{
energyBarLength = Screen.width / 5;
}
void Update()
{
DrainEnergy();
AdjustCurrentEnergy(0);
if (curEnergy < 50)
{
energyDrainSpeed = energyDrainSpeed + 100.0f;
}
if (curEnergy < 30)
{
energyDrainSpeed = energyDrainSpeed + 300.0f;
}
if (curEnergy < 20)
{
energyDrainSpeed = energyDrainSpeed + 500.0f;
}
}
void OnGUI()
{
GUI.Box(new Rect(10, 35, energyBarLength, 20), curEnergy + "/" + maxEnergy);
}
private void DrainEnergy()
{
curEnergy = Mathf.Clamp(curEnergy - (energyDrainSpeed / 12000) , 0.0f, maxEnergy);
}
public void AdjustCurrentEnergy(int adj)
{
curEnergy += adj;
if (curEnergy < 0)
curEnergy = 0;
if (curEnergy > maxEnergy)
curEnergy = maxEnergy;
if (maxEnergy < 1)
maxEnergy = 1;
energyBarLength = (Screen.width / 5) * (curEnergy / (float)maxEnergy);
}
}
}
Did you intend on your energy drain speed to stack like that from 50 to 20? If curEnergy is less than 20 then all three if statements affect energyDrainSpeed in one Update call(per frame).
Did you mean something like:
if (curEnergy <= 50 && curEnergy > 30)
{
energyDrainSpeed = energyDrainSpeed + 100.0f;
}
else if (curEnergy <= 30 && curEnergy > 20)
{
energyDrainSpeed = energyDrainSpeed + 300.0f;
}
else if (curEnergy <= 20)
{
energyDrainSpeed = energyDrainSpeed + 500.0f;
}
Yeah I did that on purpose, although I can't remember exactly why... I do remember having some reason for it though aha. I'll probably readdress that since I intend to add in a lot of other variables that influence the drain speed. but for now the issue is that it's not draining fast enough even in spite of the stacking.
Answer by high500 · Mar 03, 2016 at 07:20 PM
Am I wrong or ae you adding more frames as it goes lower? surely it should be less ie. + 100.0f, + 33.0f + 20.0f
I'm probably way off just an observation.
I'm not sure what you mean about adding more frames. are you referencing what's inside the $$anonymous$$athf.Clamp? if so, I'm subtracting from curEnergy based on the energyDrainSpeed in relation to a constant.
the drain does work and it goes down at a good rate for how I want it to drain starting from 100 energy, it's just that the equation inside $$anonymous$$athf.Clamp doesn't seem to be working when I use the energyDrainSpeed variable; the drain barely increases in speed when I run the game no matter what the curEnergy level is, unless I replace energyDrainSpeed with a constant that's equal to what energyDrainSpeed is equal to when curEnergy is below 50 or 20