- Home /
Stamina Script causes Crashing?
I've been trying to implement a Stamina system to my Sprint Script (JavaScript) but for some reason it causes my game to crash. I've pinpointed two instances that causes it to crash:
1) In the following lines of code:
while (stamina < 100)//if stamina is less than 100% { while (walking)//when walking { stamina = stamina + 0.25 * Time.deltaTime;//stamina increases at 1/4th the rate it depletes } }
Whenever I try to sprint, Unity crashes.
2) If the block above is commented out, in the following lines of code:
switch (stamina) { case 0://if stamina fully depletes recover = true;//recovery mode is enabled break; }
if (recover)//if in recovery mode
{
while (stamina < 25)//while stamina is less than 25%
{
speed = walkSpd;//walking speed is the max speed
if (stamina >= 25)//if stamina is equal to or greater than 25%
{
recover = false;//recovery mode is disabled
}
}
}</pre>
Unity crashes when Stamina reaches zero.
Here is my entire script:
#pragma strict
private var walkSpd : float = 4; //Walking speed value private var jogSpd : float = 6; //Jogging speed value
private var charMotor : CharacterMotor; private var charController : CharacterController; var stamina : int; private var recover : boolean; private var walking : boolean;
function Start () { charMotor = GetComponent(CharacterMotor); charController = GetComponent(CharacterController); stamina = 100; recover = false; walking = true; }
function Update () { var speed = walkSpd;//sets default speed to walking speed
if (charController.isGrounded && Input.GetKey(KeyCode.LeftShift) || charController.isGrounded && Input.GetKey(KeyCode.RightShift))
{ //if the player's character is on the ground AND he/she is pressing a SHIFT key
speed = jogSpd; //the player moves at jogging speed
stamina = stamina - 1 * Time.deltaTime; //stamina decreases over time
walking = false; //the player is not walking
}
else //if the player is NOT on the ground AND holding a SHIFT key
{
/*speed = walkSpd;*/ //the player's speed is walking speed
walking = true; //the player is walking
}
switch (stamina)
{
case 0://if stamina fully depletes
recover = true;//recovery mode is enabled
break;
}
if (recover)//if in recovery mode
{
while (stamina < 25)//while stamina is less than 25%
{
speed = walkSpd;//walking speed is the max speed
if (stamina >= 25)//if stamina is equal to or greater than 25%
{
recover = false;//recovery mode is disabled
}
}
}
while (stamina < 100)//if stamina is less than 100%
{
while (walking)//when walking
{
stamina = stamina + 0.25 * Time.deltaTime;//stamina increases at 1/4th the rate it depletes
}
}
charMotor.movement.maxForwardSpeed = speed; //Setting new speed variable under appropriate conditions
}
What am I doing wrong?
For the second instance, I've used an "if" statement as well, but I get the same result.
After tinkering a bit more, it seems that Unity does not like "While" statements very much. So, for an updated Script, change wherever it says "when" to "if". That fixed the crashing problem, but now my sta$$anonymous$$a does not regenerate.
Answer by robertbu · Jun 15, 2013 at 07:14 AM
Yep, this will hang. So you start sprinting, the stamina drops below 100, so you do the while (stamina < 100) loop. But you are not walking, so stamina is never incremented, so you are stuck in the outer loop.
The solution is to not use the while() loops. In fact the way you have it structured, stamina will in a single frame go to 100. Update() gets called every frame, so you just want to do a bit of work each frame:
if (stamina < 100) {
if (walking) {
stamina += 0.25 * Time.deltaTime;
}
}
And you want to do the same for your other while loop:
if (stamina < 25) {
...
}
ok, but if you read my comments I said I fixed the crashing problem by doing exactly what you said but figured it out on my own. Now my sta$$anonymous$$a does not regenerate and now I need help with THAT.
Thanks though.
You declared sta$$anonymous$$a and int, but you do floating point calculations with sta$$anonymous$$a. You need to clean these up. In particular when you add .25 to a int, it is truncated back to its original value.
A simple check like below could regenerate sta$$anonymous$$a over a given rate:
if(sta$$anonymous$$a <= 0)
{
sta$$anonymous$$a += Time.deltaTime / 2; // change the 2 to alter re-gen speed.
}
Ugh, wow. I completely missed that. I'll try this and see what comes up.
Answer by Jamora · Jun 16, 2013 at 02:55 PM
You should read up on Coroutines. http://docs.unity3d.com/Documentation/ScriptReference/index.Coroutines_26_Yield.html I try to have all my while loops in coroutines, because then my editor won't crash if I happen to have made an infinite loop (happens a lot).
I would start a coroutine to decrease stamina whenever a character starts moving, and another to regenerate stamina whenever it's appropriate. But all put together the coroutine would look something like this (untested)
function UpdateStamina(){
if (stamina <= 0)//if stamina fully depletes
{
recover = true;//recovery mode is enabled
yield;
}
if (recover)//if in recovery mode
{
while (stamina < 25)//while stamina is less than 25%
{
speed = walkSpd;//walking speed is the max speed
if (stamina >= 25)//if stamina is equal to or greater than 25%
{
recover = false;//recovery mode is disabled
}
yield;
}
}
while (stamina < 100 && walking)//if stamina is less than 100%
{
stamina = stamina + 0.25 * Time.deltaTime;//stamina increases at 1/4th the rate it depletes
yield;
}
}
Remove that section in Update and put StartCoroutine("UpdateStamina");
in Start()
Ah, ok. By the way, what exactly does "yield;" do? I have a pretty good idea, but I just want to be sure.
You don't want to use yield here. You just want to have your routine called once per frame.
Sta$$anonymous$$a STILL isn't regenerating. If I stop running it'll stop going down, but the sta$$anonymous$$a value still is not going up. I even set the rate of depletion (which is pretty fast) equal to and higher than the rate of increase.
I think for some reason it's not recognizing that sta$$anonymous$$a is less than 100.
And now my character isn't moving at the correct speed.
I'm fed up with this stupid script. NOTHING works.
You did convert sta$$anonymous$$a to a float? If you put a Debug.Log() statement at the point where sta$$anonymous$$a should be added, does it fire?
I just started over and am trying something entirely different. I've split Sprint and Sta$$anonymous$$a into their own scripts and will try that ins$$anonymous$$d. Plus, I've never used Unity before and don't fully understand how to utilize Unity yet. I don't understand how to use "Debug"s, or "yield"s, and lots of other stuff that I don't even know exist yet. Plus, the stupid "documentation" rarely helps me and the same goes for the videos/tutorial things. I have an intermediate understanding of program$$anonymous$$g and can figure most bugs out, but I've never worked with an Engine before so I'm really, REALLY frustrated and have lost most of my patience, so forgive me if I come off a bit curt.
Your answer
Follow this Question
Related Questions
Whats a good sprinting script? 1 Answer
Modifying the Character Motor Script 1 Answer
GUI.Box that will appear for 3sec 1 Answer
Increasing And Decreasing An Array Through A Variable 2 Answers
GUI.Label Not Disappearing 1 Answer