- Home /
Question by
WhisperXD · Aug 27, 2018 at 06:55 AM ·
floatreducesubtracting
how to find out if the float has been successfully reduced ??
hi everyone, i wanna ask about reducing float, then stop reducing when float has been successfully reduced by as much reduceValue,, but i still use Time.deltatime to reduce
public float health, reduceValue;
private void Update()
{
health -= reduceValue * Time.deltaTime;
//how can i know health has been reduced by as much reduceValue
//then health stop reducing or reduceValue change to 0 ??
if (Input.GetKeyDown(KeyCode.X))
{
reduceValue = 5;
}
}
thanks,,
Comment
Best Answer
Answer by Hellium · Aug 27, 2018 at 07:36 AM
public float health ;
private float reduceValue;
private float remainingReduceValue;
private void Update()
{
float reduceDelta = Mathf.Min( reduceValue * Time.deltaTime, remainingReduceValue );
remainingReduceValue -= reduceDelta ;
health -= reduceDelta ;
if( remainingReduceValue <= Mathf.Epsilon )
{
reduceValue = 0;
Debug.Log("Health decreased !");
}
if (Input.GetKeyDown(KeyCode.X))
{
reduceValue = remainingReduceValue = 5;
}
}