- Home /
Why aren't these numbers going down by 1.
Hey. So I got this script to show stamina in my game. It's in C# and I'm not good at it. If found it at a forum and added some bits to it to fit me. One of which is displaying the stamina, at the bottom in GUI. The problem I have is that when I start running the number goes down by decimals and not whole numbers so the whole thing looks weird on screen. How do I make it so it simply goes down 1 by 1 and not in decimals.
using UnityEngine;
using System.Collections;
public class StaminaRun : MonoBehaviour
{
private float barLength = 0.0f;
// Max running time
public float RunningTime = 10f;
// How fast do we regenerate available running time?
public float RestRate = 1f;
// Player must be able to run for at least X seconds
public float RunThreshold = 2f;
private vp_FPPlayerEventHandler Player;
public float availableRunTime;
protected virtual void Awake()
{
// cache the player event handler
Player = GameObject.FindObjectOfType(typeof(vp_FPPlayerEventHandler)) as vp_FPPlayerEventHandler;
availableRunTime = RunningTime;
}
/// <summary>
/// registers this component with the event handler (if any)
/// </summary>
protected virtual void OnEnable()
{
// allow this monobehaviour to talk to the player event handler
if (Player != null)
Player.Register(this);
}
/// <summary>
/// unregisters this component from the event handler (if any)
/// </summary>
protected virtual void OnDisable()
{
// unregister this monobehaviour from the player event handler
if (Player != null)
Player.Unregister(this);
}
public void Update()
{
if (!Player.Run.Active)
{
// If we're not running, regenerate our stamina
availableRunTime = Mathf.Min(availableRunTime + Time.deltaTime * RestRate, RunningTime);
return;
}
// Decrease our running time available and see if we have to stop
availableRunTime -= Time.deltaTime;
if (availableRunTime <= 0)
{
Player.Run.TryStop();
availableRunTime = 0;
}
}
protected virtual bool CanStart_Run()
{
// Only enable running if we're above the running threshold
if (availableRunTime >= RunThreshold)
return true;
return false;
}
void OnGUI ()
{
GUI.Box(new Rect(5, 130, 55, 23), "Stamina");
GUI.Box(new Rect(62, 130, barLength, 23), availableRunTime + "/" + 10);
}
void Start ()
{
barLength = Screen.width / 7;
}
}
I'll give you some hints...
This line
availableRunTime -= Time.deltaTime;
each time its run decreases the value in availableRunTime by the value Time.deltaTime
Time.deltaTime is a FLOAT (decimal)
Google search :
Unity Time.deltaTime, int, float, parse.
The last one can help you quickly but not efficiently. I could spell it out but you'll learn more this way...
I searched it up and I got availableRunTime -= Time.deltaTime / 1; I already tried that but no luck.
1/1 = 1
200/1 = 200
/ 1 = 0.1
2000258.267 / 1 = 2000258.267
See where I'm going with this?
Answer by DylanW · Oct 13, 2013 at 10:53 PM
This is because Time.deltaTime is a float. Try using Mathf.FloorToInt() to calculate it as an int, which is a whole number.
Thanks, I got it half working. currentSta$$anonymous$$a = $$anonymous$$athf.FloorToInt(availableRunTime);
I put that into the update and it works when sta$$anonymous$$a goes down but when it goes up it doesn't do anything. 'currentSta$$anonymous$$a' is a int var.
Your answer
Follow this Question
Related Questions
Golf swinging gauge - Techanically any type of recepricating gauge. 0 Answers
int myX = (int)transform.position.x; 1 Answer
Rounded particle motion 0 Answers
Floating Point and ToString issue 2 Answers
GUI.HorizontalSlider precision 3 Answers