Question by
ZWLSOFTWARE · Aug 21, 2019 at 11:19 AM ·
inttime.deltatime
Is it possible to Round Time.deltaTime to int?
Hi!
I'm new to coding/unity, is it possible to round time.deltatime to a int value? i don't want my health to be with decimals like ( 20.545 )
int health;
health += 1 * Mathf.RoundToInt(Time.deltaTime);
Comment
Best Answer
Answer by Hellium · Aug 21, 2019 at 11:26 AM
Mathf.RoundToInt
will most likely 0
all the time since it's (supposed to be) a very small value (< 0.33 for 30 FPS).
I would go for an additional float value storing the health as a float you will round afterwards:
private int health;
private float decimalHealth;
// ....
decimalHealth += 1 * Time.deltaTime;
health = Mathf.RoundToInt( decimalHealth ) ;
Little note though, if you want to modify the health from somewhere else (to damage the player for instanc)e, you will need to modify the decimalHealth
ins$$anonymous$$d and round the health afterwards.
I highly advise you to use functions to manage the health:
private int health;
private float decimalHealth;
// ....
Heal( 1 * Time.deltaTime ) ;
public void Heal( float amount )
{
if( amount < 0 ) throw new System.ArgumentException("Amount must be greater than 0");
decimalHealth += amount;
health = $$anonymous$$athf.RoundToInt( decimalHealth ) ;
}
public void Damage( float amount )
{
if( amount < 0 ) throw new System.ArgumentException("Amount must be greater than 0");
decimalHealth -= amount;
health = $$anonymous$$athf.RoundToInt( decimalHealth ) ;
}