- Home /
Speed vs Fatigue
I have a character that needs to fatigue as he runs, requiring the player to stop running in order for the fatigue meter to recharge.
How can I go about doing this? Do I need to make a special GUI or something? Please let me know, my scripting abilities are limited and I'm trying to learn. I would greatly appreciate it! Thank you!
Answer by Jesse Anders · Mar 07, 2011 at 02:59 PM
First, you need to separate the 'simulation' aspect from the 'UI' aspect (think 'model-view'). These are two separate problems, and trying to solve them at the same time will just lead to confusion.
The UI part is probably the easier of the two aspects of the problem. I don't know what you want to display exactly (a number representing the current 'fatigue' factor maybe?), but at its simplest, you can just use GUI.Label() or GUILayout.Label() to display a numerical value.
Tracking fatigue and responding accordingly is the trickier part. As is usually the case, there's more than one way it can be done, but here's how I'd probably proceed:
- If the player is running, increase the fatigue factor, using Time.deltaTime to scale the increase rate
- If the player is not running, decrease the fatigue factor (clamping at 0), using Time.deltaTime to scale the decrease rate
- If the player is running and the fatigue factor reaches the maximum (assuming that's how you want it to work), disable running for the player until the fatigue factor has gone down by some amount
Answer by dissidently · Mar 07, 2011 at 03:03 PM
You'll need someone with real scripting skills, which I don't have. I can only give a theoretical answer.
You'll need a boolean variable that stores [player_is_running: TRUE/FALSE]
When [player_is_running = TRUE] you need a function that negatively impacts the players Fatigue (another variable) across the time that the player is running.
When [player_is_running = FALSE] you need to activate another function that impacts positively upon the Fatigue variable.
The above to can be tested for in an Update function upon the player character object, probably in a If, then, else loop.
Sorry I can't be of more help. Someone with real coding skills will likely be along shortly to shoot down my ideas and show you how it best be done.