Reputation: 13
The game character will lose hunger when he moved. I tried to use Mathf.Clamp
to limit the number when the player consumed food which will add 20 hunger to the HungerClock
so it doesn't go more than 100. The problem I have is the number in HUD will not display the number that's more than a hundred but the system will stored the number that goes up to 100. So when the number is below 100 the hud will start updating the number.
This is part of my code:
private float HungerClock;
private float ThirstClock;
private float MaxHunger = 100.0f;
private float MinHunger = 0.0f;
private float HungerStatus;
void Start ()
{
thePlayer = GetComponent<Player> ();
health = thePlayer.ReturnHealth ();
player = GameObject.Find("Player");
HungerClock = thePlayer.ReturnHunger();
}
void Update () {
HungerStatus = Mathf.Clamp(HungerStatus, MinHunger, MaxHunger);
HungerClock = Mathf.Clamp(HungerClock, MinHunger, MaxHunger);
PlayerHUD.updateHunger(HungerStatus);
PlayerHUD.updateThirst(ThirstStatus);
if(sceneName == ("Bunker"))
{
HungerStatus = 100.0f;
ThirstStatus = 100.0f;
}
else
{
//Hunger and thirst counter-----------------------------
HungerStatus = HungerClock - distanceTravelled * 0.5f;
ThirstStatus = ThirstClock - distanceTravelled * 0.5f;
distanceTravelled += Vector3.Distance(transform.position, lastPosition);
lastPosition = transform.position;
//------------------------------------------------------
}
}
public void ChangeHunger(float change)
{
HungerClock += change;
// I tried to put Mathf.Clamp here but it did not work either.
}
HUD Script
public void updateHunger (float Hunger)
{
//Hunger = Player.ReturnHunger();
hungerTextMesh.text = hungerText + Hunger;
Upvotes: 1
Views: 770
Reputation: 951
I think the other responders tackled this with more thought. And @Adam above actually mentioned it, but to elaborate on his solution, try this:
void Update () {
if(sceneName == ("Bunker"))
{
HungerStatus = 100.0f;
ThirstStatus = 100.0f;
}
else
{
//Hunger and thirst counter-----------------------------
HungerStatus = HungerClock - distanceTravelled * 0.5f;
ThirstStatus = ThirstClock - distanceTravelled * 0.5f;
distanceTravelled += Vector3.Distance(transform.position, lastPosition);
lastPosition = transform.position;
//------------------------------------------------------
}
HungerStatus = Mathf.Clamp(HungerStatus, MinHunger, MaxHunger);
HungerClock = Mathf.Clamp(HungerClock, MinHunger, MaxHunger);
PlayerHUD.updateHunger(HungerStatus);
PlayerHUD.updateThirst(ThirstStatus);
}
Notice the change in order of statements. We are now using the Mathf.Clamp() after all the other processing. I hope that helps!
Upvotes: 0
Reputation: 33146
It is going over 100 because you told it to.
You have written:
So ... now the value in HUD and the value in the game are different.. And the HUD will never go above 100 because you clamped it (step 1), but the game will go above 100 because you changed it without clamping it (step 4)
Upvotes: 1