Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Help me! Sprint Stamina!

Discussion in 'Scripting' started by RuslanDavletshin, Aug 26, 2012.

  1. RuslanDavletshin

    RuslanDavletshin

    Joined:
    Aug 26, 2012
    Posts:
    12
    Hi, guys. I want to be when the character runs, reduced his stamina. Also, I need to play sounds of dyspnea after endurance run low. There are three sounds, their average - about one second. Thanks.

    Code (csharp):
    1.  
    2. var walkSpeed: float = 2.5;
    3. var crchSpeed: float = 1.25;
    4. var runSpeed: float = 5;
    5.  
    6. private var chMotor: CharacterMotor;
    7. private var tr: Transform;
    8. private var dist: float;
    9.  
    10. function Start(){
    11.     chMotor = GetComponent(CharacterMotor);
    12.     tr = transform;
    13.     var ch:CharacterController = GetComponent(CharacterController);
    14.     dist = ch.height/2;
    15. }
    16.  
    17. function Update(){
    18.  
    19.     var vScale = 1.0;
    20.     var speed = walkSpeed; 
    21.     var walksoundchange: walksound = GetComponent(walksound);
    22.     walksoundchange.waitTime = 0.6;    
    23.    
    24.     if (Input.GetKey("left shift")){
    25.         speed = runSpeed;
    26.         walksoundchange.waitTime = 0.3;
    27.     }
    28.     if (Input.GetKey("left ctrl")){
    29.         vScale = 0.8;
    30.         speed = crchSpeed;
    31.         walksoundchange.waitTime = 0.9;
    32.     }
    33.     chMotor.movement.maxForwardSpeed = speed;
    34.     var ultScale = tr.localScale.y;  
    35.     tr.localScale.y = Mathf.Lerp(tr.localScale.y, vScale, 5*Time.deltaTime);
    36.     tr.position.y += dist * (tr.localScale.y-ultScale);
    37. }
     
    Last edited: Aug 26, 2012
  2. RuslanDavletshin

    RuslanDavletshin

    Joined:
    Aug 26, 2012
    Posts:
    12
    HEELP! I've got to finish the game!
     
  3. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    I cant make sense of your script very easily, although it looks like some if statements have non closed parenthesis. Is the green code just commented out?

    For an idea, once the character starts running, there could be a mathf.movetowards function for the stamina that moves the stamina towards zero over time. Then once it gets to zero, the sprinting does not work.
     
  4. RuslanDavletshin

    RuslanDavletshin

    Joined:
    Aug 26, 2012
    Posts:
    12
    Roger, i do not know how to write a script, help me, please.

    edited script:

     
  5. RuslanDavletshin

    RuslanDavletshin

    Joined:
    Aug 26, 2012
    Posts:
    12
  6. cyberianhd

    cyberianhd

    Joined:
    Aug 20, 2012
    Posts:
    51
    i need the exact same script, i would be grateful also if anybody could help him and me out.
     
  7. RuslanDavletshin

    RuslanDavletshin

    Joined:
    Aug 26, 2012
    Posts:
    12
    evil people here)
     
  8. rob4097

    rob4097

    Joined:
    May 15, 2012
    Posts:
    50
    If you're not familiar with creating a script, here's a great place to start:

    http://forum.unity3d.com/threads/51017-5-Unity-game-examples-C-scripting-tutorial-for-beginners

    For your specific case, the workflow would be:

    1. Use a boolean (true/false) variable to determine if the player CAN sprint, and another to determine if he was sprinting or walking. If the walk key is held down, for example, the player would move but the variable (I'll cal it 'isSprinting') would be false. If the sprint key is held down, the player would move faster and isSprinting is true.
    2. While isSprinting is true, either:
    a) Use a timer to count down the number of seconds until the player can't sprint anymore, or
    b) Lerp a value from x towards 0, where x is the number of "ticks" left until you can no longer sprint. Each frame removes one tick.
    3. When either a) or b) has run its course, set isSprinting to false, and then canSprint to false as well. Use a timer to count down until they can sprint again, and then change canSprint to true.

    Some links, to get you started since the above is not code.

    http://docs.unity3d.com/Documentation/ScriptReference/Mathf.Lerp.html
    http://docs.unity3d.com/Documentation/ScriptReference/Time.html

    Good luck!

    Edit:

    For the audio, just play/stop the audiosource as needed when you change the state between sprinting/walking.

    http://docs.unity3d.com/Documentation/ScriptReference/AudioSource.html
     
    Last edited: Aug 27, 2012
  9. cyberianhd

    cyberianhd

    Joined:
    Aug 20, 2012
    Posts:
    51
    i am going to start on the script if i can actually get it working i will pm you or post it here. Audio will be the last thing i work on though so sorry if the script dosnt have audio settings.
     
  10. RuslanDavletshin

    RuslanDavletshin

    Joined:
    Aug 26, 2012
    Posts:
    12
    Thanks, but I have no time. You are so hard to change and show me the script?
     
  11. Loius

    Loius

    Joined:
    Aug 16, 2012
    Posts:
    546
    Sounds pretty simple to me.

    Code (csharp):
    1. private var stamina : float;
    2. private var nextCanRunTime : float;
    3. private var nextStaminaRechargeTime : float;
    4. var staminaPenaltyTime : float = 2.0;
    5. var staminaRechargeDelay : float = 1.0;
    6. var maxStamina : float = 4.0;
    7.  
    8. var exhaustionClip : AudioClip[];
    9.  
    10. function Update() {
    11.   if ( /* trying to run */  Time.time > nextCanRunTime ) {
    12.     stamina-= Time.deltaTime;
    13.     nextStaminaRechargeTime = Time.time + staminaRechargeDelay;
    14.     if ( stamina > 0 ) {
    15.       /* do run */
    16.     } else {
    17.       nextCanRunTime = Time.time + staminaPenaltyTime;
    18.       audioSource.PlayOneShot( exhaustionClip[ ChooseClip() ] );
    19.     }
    20.   } else if ( Time.time > nextStaminaRechargeTime ) {
    21.     stamina= Mathf.Min(stamina+Time.deltaTime, maxStamina);
    22.   }
    23. }
    24.  
    25. function ChooseClip() : AudioClip {
    26.   return exhaustionClip[ Random.Range(0,exhaustionClip.length) ];
    27. }
    28.  
     
  12. Eiznek

    Eiznek

    Joined:
    Jun 9, 2011
    Posts:
    374
    Do you guys even have Stamina in place? Vitals? Here's an example of how you can handle it. I don't program in js however so forgive me if it has errors.

    Code (csharp):
    1.  
    2.     var walkSpeed: float = 2.5;
    3.     var crchSpeed: float = 1.25;
    4.     var runSpeed: float = 5;
    5.      
    6.     private var chMotor: CharacterMotor;
    7.     private var tr: Transform;
    8.     private var dist: float;
    9.      
    10.     function Start(){
    11.         chMotor = GetComponent(CharacterMotor);
    12.         tr = transform;
    13.         var ch:CharacterController = GetComponent(CharacterController);
    14.         dist = ch.height/2;
    15.     }
    16.      
    17.    float minThreashold = 0.25f; //25%.
    18.     function Update(){
    19.      
    20.        if(playerStamina < playerStamina*minThreashold)  // 100*0.25 = 25 stamina.
    21.        {
    22.            //Play exhausted sound.
    23.        }
    24.         var vScale = 1.0;
    25.         var speed = walkSpeed;
    26.         var walksoundchange: walksound = GetComponent(walksound);
    27.         walksoundchange.waitTime = 0.6;    
    28.        
    29.         if (Input.GetKey("left shift")){
    30.             speed = runSpeed;
    31.  
    32.            StartCoroutine(DrainStamina() );  //We start a coroutine that will drain stam every specified time while its being held down.
    33.  
    34.             walksoundchange.waitTime = 0.3;
    35.         }
    36.        if (Input.GetKeyUp("left shift"))  //On Key Up We stop draining.
    37.        {
    38.          staminaDrain = false;
    39.        }
    40.         if (Input.GetKey("left ctrl")){
    41.             vScale = 0.8;
    42.             speed = crchSpeed;
    43.             walksoundchange.waitTime = 0.9;
    44.         }
    45.         chMotor.movement.maxForwardSpeed = speed;
    46.         var ultScale = tr.localScale.y;  
    47.         tr.localScale.y = Mathf.Lerp(tr.localScale.y, vScale, 5*Time.deltaTime);
    48.         tr.position.y += dist * (tr.localScale.y-ultScale);
    49.     }
    50.    
    51.    float playerStamina = 100;
    52.    bool drainStam = false;
    53.    float delayUntilDrain = 0.5f;
    54.    function DrainStamina()
    55.    {
    56.         drainStam = true;
    57.         while(drainStam)
    58.         {
    59.              playerStamina -= 1;
    60.              yield return WaitForSeconds(delayUntilDrain);
    61.         }
    62.    }
    63.  
     
  13. RuslanDavletshin

    RuslanDavletshin

    Joined:
    Aug 26, 2012
    Posts:
    12
    Many thanks to all!
     
  14. RuslanDavletshin

    RuslanDavletshin

    Joined:
    Aug 26, 2012
    Posts:
    12
    Eiznek, i'm ashamed to say it, but this script does not work: (
     
  15. imjacobf

    imjacobf

    Joined:
    Aug 3, 2012
    Posts:
    39
    Work on a simpler project until you understand scripting, then. What he gave you may not work if you copy/paste it into your project, but you should be able to read through it and see how he is doing it (think of it like pseudocode) and then come up with a solution to fit your own needs.
     
  16. RuslanDavletshin

    RuslanDavletshin

    Joined:
    Aug 26, 2012
    Posts:
    12
    I understand code, but do not know what kind of errors in it.

    CODE:

    Code (csharp):
    1.  
    2. var walkSpeed: float= 2.5;
    3. var crchSpeed: float= 1.25;
    4. var runSpeed: float= 5;
    5.  
    6. private var chMotor: CharacterMotor;
    7. private var tr: Transform;
    8. private var dist: float;
    9.  
    10.  
    11. function Start ( )
    12.  {
    13.  
    14.   chMotor = GetComponent ( CharacterMotor );
    15.   tr= transform;
    16.   var ch: CharacterController= GetComponent ( CharacterController );
    17.   dist= ch. height/2;
    18.  
    19.  }
    20.  
    21. var minThreashold: float= 0.20f;
    22. var playerStamina: float= 100;
    23. var drainStam: boolean= false;
    24. var delayUntilDrain: float= 0.5f;
    25.  
    26. function Update ( )
    27.  {
    28.  
    29.   if ( playerStamina < playerStamina* minThreashold )
    30.    {
    31.     // start loop
    32.    }
    33.  
    34.   if ( playerStamina > playerStamina* minThreashold )
    35.    {
    36.     // stop loop
    37.    }
    38.  
    39.   var vScale= 1.0;
    40.   var speed= walkSpeed;  
    41.   var walksoundchange: walksound= GetComponent ( walksound );
    42.   walksoundchange. waitTime= 0.6;    
    43.  
    44.   if ( Input. GetKey ( "left shift" ) )
    45.    {
    46.     speed= runSpeed;
    47.     StartCoroutine ( DrainStamina ( ) );
    48.     walksoundchange. waitTime= 0.3;
    49.    }
    50.    
    51.   if ( Input. GetKeyUp ( "left shift" ) )
    52.    {
    53.     drainStam = false;
    54.    }
    55.   if ( Input. GetKey ( "left ctrl" ) )
    56.    {
    57.     vScale= 0.8;
    58.     speed= crchSpeed;
    59.     walksoundchange. waitTime= 0.9;
    60.    }
    61.  
    62.   chMotor. movement. maxForwardSpeed= speed;
    63.   var ultScale= tr. localScale. y;  
    64.   tr. localScale. y= Mathf. Lerp( tr. localScale. y, vScale, 5* Time. deltaTime );
    65.   tr. position. y+= dist* (tr. localScale. y- ultScale);
    66.  }
    67. function DrainStamina ( )
    68.  {
    69.   drainStam= true;
    70.        
    71.    while ( drainStam )
    72.     {
    73.      playerStamina -= 1;
    74.      yield;
    75.      return WaitForSeconds ( delayUntilDrain );
    76.     }
    77.  }
    78.  
    SCREENSHOT WITH ERROR:

     
    Last edited: Aug 28, 2012
  17. imjacobf

    imjacobf

    Joined:
    Aug 3, 2012
    Posts:
    39
    The problem is you are trying to declare another function inside of an existing one. Move the code:

    Code (csharp):
    1.  
    2. function DrainStamina()
    3. {
    4.      <code snipped>
    5. }
    6.  
    outside of the Update() function.
     
  18. RuslanDavletshin

    RuslanDavletshin

    Joined:
    Aug 26, 2012
    Posts:
    12
    I edited the previous message because the error occurred.
     
  19. imjacobf

    imjacobf

    Joined:
    Aug 3, 2012
    Posts:
    39
    And...? Did you get a new error?
     
  20. RuslanDavletshin

    RuslanDavletshin

    Joined:
    Aug 26, 2012
    Posts:
    12
    See previous post.
     
  21. imjacobf

    imjacobf

    Joined:
    Aug 3, 2012
    Posts:
    39
    Ah, replace the DrainStamina() function with this:
    Code (csharp):
    1.  
    2. function DrainStamina()
    3. {
    4.     drainStam= true;
    5.  
    6.     while (drainStam)
    7.     {
    8.     playerStamina -= 1;
    9.     yield WaitForSeconds(delayUntilDrain);
    10.     }
    11. }
    12.  
     
  22. RuslanDavletshin

    RuslanDavletshin

    Joined:
    Aug 26, 2012
    Posts:
    12
    Thank you!
    How to make sure that when stamina ends, the character back to the original speed?
     
    Last edited: Aug 28, 2012