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;
}
}