Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MrMcKinle · Jun 15, 2013 at 07:08 AM · javascriptcrashsprint

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MrMcKinle · Jun 15, 2013 at 07:07 AM 0
Share

For the second instance, I've used an "if" statement as well, but I get the same result.

avatar image MrMcKinle · Jun 15, 2013 at 07:26 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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) {
     ...
 
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MrMcKinle · Jun 16, 2013 at 07:57 AM 0
Share

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.

avatar image robertbu · Jun 16, 2013 at 02:25 PM 0
Share

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.

avatar image samtperrin · Jun 16, 2013 at 03:48 PM 0
Share

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.
 }
avatar image MrMcKinle · Jun 17, 2013 at 03:49 AM 0
Share

Ugh, wow. I completely missed that. I'll try this and see what comes up.

avatar image
1

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()

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MrMcKinle · Jun 17, 2013 at 03:48 AM 0
Share

Ah, ok. By the way, what exactly does "yield;" do? I have a pretty good idea, but I just want to be sure.

avatar image robertbu · Jun 17, 2013 at 04:08 AM 0
Share

You don't want to use yield here. You just want to have your routine called once per frame.

avatar image MrMcKinle · Jun 17, 2013 at 04:22 AM 0
Share

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.

avatar image robertbu · Jun 17, 2013 at 05:21 AM 0
Share

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?

avatar image MrMcKinle · Jun 17, 2013 at 09:59 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges