apply something on enum

Hey,
Here is the issue, i have some states on the player’s hunger. Depending on his current state, diferente things will happen, for exemple if he is hungry, he moves 50% slower. Here is the code for that

    CurrentNourishment ReverseNourishment()
    {
        if(CurHunger < 5)
        {
            currentNourishment = CurrentNourishment.Nourished;
        }
        if(CurHunger <= 50 && CurHunger > 5)
        {
            currentNourishment = CurrentNourishment.Fine;
        }
        if(CurHunger <= 75 && CurHunger > 50)
        {
            currentNourishment = CurrentNourishment.Hungry;
        }
        if(CurHunger > 75)
        {
            currentNourishment = CurrentNourishment.Starving;
        }
        return currentNourishment;
    }

I thought about using a While, but it kept crashing, i thought about a switch, but that repeated the whole thing several times, so if i put curSpeed–; it would keep happening until he couldn’t even move, when in reality, i only want that to happen once. I understand why this happens, but i dont know how to fix it. Here is my update.

    void Update()
    {

        if(!isDead)
        {
            IncreaseHunger();
            IncreaseThirst();
            ReverseNourishment();
        }
    }

I understand that it runs several times on update until the condition is not met, but how can i fix this?

Rather than directly reducing your “curSpeed” value, you could use your nourishment as a multiplier for your speed.

For example:

float curSpeed;
float baseSpeed = 5.0f;
float nourishmentMult;

CurrentNourishment ReverseNourishment()
{
	// If the function returns something, might as well make use of it
	CurrentNourishment newNourishment;
	// Since the same value is being tested throughout,
	// might as well save checks by using "else if"
	if(CurHunger < 5)
	{
		newNourishment = CurrentNourishment.Nourished;
		nourishmentMult = 1.0f; // Don't reduce speed
	}
	else if(CurHunger <= 50 && CurHunger > 5)
	{
		newNourishment = CurrentNourishment.Fine;
		nourishmentMult = 0.95f; // Reduce speed slightly
	}
	else if(CurHunger <= 75 && CurHunger > 50)
	{
		newNourishment = CurrentNourishment.Hungry;
		nourishmentMult = 0.75f; // Reduce speed moderately
	}
	else// if(CurHunger > 75) // This condition is currently implied
	{
		newNourishment = CurrentNourishment.Starving;
		nourishmentMult = 0.5f; // Reduce speed significantly
	}
	CalculateSpeed();
	return newNourishment;
}

void CalculateSpeed()
{
	curSpeed = baseSpeed * nourishmentMult * otherMultA * otherMultB;
}

void Update()
{
	// ...
	currentNourishment = ReverseNourishment();
	// ...
}

I threw in a few example values as a point of reference, but the main point to be made here is that you simply need fixed conditions to dictate your speed if you want reasonable control over it. You can still scale it relative to the current point in a given hunger level (Lerp()) as desired.