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 Entyro · Apr 10, 2014 at 08:33 AM · guibarstamina

Stamina Bar Help

Hi!

I have a working script for a walking and sprinting mechanism. The thing I was wondering about was how to make a stamina bar that slowly goes down when you're running and when you've stopped running it goes slowly up again.

Here's my script:

 #pragma strict
 @script RequireComponent( AudioSource )
  
 var walkSounds : AudioClip[];
 var runSounds : AudioClip[];
  
 var walkAudioSpeed : float = 0.4;
 var runAudioSpeed : float = 0.2;
  
 private var walkAudioTimer : float = 0.0;
 private var runAudioTimer : float = 0.0;
  
 var isWalking : boolean = false;
 var isRunning : boolean = false;
  
 var walkSpeed : float = 8; // regular speed
 var runSpeed : float = 20; // run speed
  
 var sprintDuration : float = 10.0;
 private var sprintTimer : float = 0;
  
 private var chCtrl: CharacterController;
 private var chMotor: CharacterMotor;
  
 function Start()
 {
     chCtrl = GetComponent(CharacterController);
     chMotor = GetComponent(CharacterMotor);
 }
  
 function Update()
 {
     SetSpeed();
  
     if ( chCtrl.isGrounded )
     {
        PlayFootsteps();
     }
     else
     {
        walkAudioTimer = 1000.0;
        runAudioTimer = 1000.0;
     }
 }
  
 function SetSpeed()
 {
     var speed = walkSpeed;
  
     if ( Input.GetAxis( "Horizontal" ) || Input.GetAxis( "Vertical" ) )
     {
        if ( Input.GetKey( "left shift" ) || Input.GetKey( "right shift" ) )
        {
          // Running
          isWalking = false;
          isRunning = true;
        }
        else
        {
          // Walking
          isWalking = true;
          isRunning = false;
        }
     }
     else
     {
        // Stopped
        isWalking = false;
        isRunning = false;
     }
  
     // check the sprint timer
     if ( isRunning )
     {
        sprintTimer += Time.deltaTime;
  
        if ( sprintTimer > sprintDuration )
        {
          isRunning = false;
          isWalking = true;
        }
        else if ( chCtrl.isGrounded )
        {
          speed = runSpeed;
        }
     }
     else
     {
        sprintTimer -= Time.deltaTime;
     }
  
     chMotor.movement.maxForwardSpeed = speed;
 }
  
 function PlayFootsteps()
 {
     // Play Audio
     if ( isWalking )
     {
        if ( walkAudioTimer > walkAudioSpeed )
        {
          audio.Stop();
          audio.clip = walkSounds[ Random.Range( 0, walkSounds.Length ) ];
          audio.Play();
          walkAudioTimer = 0.0;
        }
     }
     else if ( isRunning )
     {
        if ( runAudioTimer > runAudioSpeed )
        {
          audio.Stop();
          audio.clip = runSounds[ Random.Range( 0, runSounds.Length ) ];
          audio.Play();
          runAudioTimer = 0.0;
        }
     }
     else
     {
        audio.Stop();
     }
  
     // increment timers
     walkAudioTimer += Time.deltaTime;
     runAudioTimer += Time.deltaTime;
 }

Does anyone know how to add this?

Comment
Add comment · Show 3
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 BraveVN · Apr 10, 2014 at 08:46 AM 0
Share

Google is your best friend when you have troubles: http://hdmark.com.au/tutorials/sta$$anonymous$$abar

avatar image AlucardJay · Apr 10, 2014 at 09:47 AM 0
Share

Nice script, looks familiar ;)

Just create a normalized value with

 sprintTimer / sprintDuration;

Then use that as a modifier for a GUI bar, whatever ...

avatar image Entyro · Apr 10, 2014 at 01:54 PM 0
Share

Thanks for the script, it's awesome! :D I'm not the best at scripting things so can you please tell me how to do it? I've added this code for the gui:

 function OnGUI()
 {
     GUI.Label (Rect (Screen.width/2-160, Screen.height/2-450, 400, 50), sta$$anonymous$$aTexture);
 }

Now what?

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by AlucardJay · Apr 10, 2014 at 07:10 PM

I modified the script a little as I never tested it for that answer, and found some strange things happening with the sprint timer going out of bounds.

Now there is a line in the function SetSpeed that clamps the sprint timer :

 sprintTimer = Mathf.Clamp( sprintTimer, 0, sprintDuration );

When I say create a normalized value, this means create a value between 0 and 1 that represents the percentage of stamina available. With this percentage, you can modify another value to represent that percentage, eg :

 stamina = sprintTimer / sprintDuration;

You can use this value in the width of the GUI bar :

 GUI.DrawTexture( Rect( 30, Screen.height - 80, 400 * stamina, 50 ), staminaTexture );

so if stamina is full, the width will be 400; if the stamina is empty, the width will be 0.

but the sprint timer is backwards (max when no stamina, 0 when full stamina), so the stamina line needs to be inverted :

 stamina = 1.0 - ( sprintTimer / sprintDuration );

Since I have modified a few things in the original script, here is the complete code with a stamina bar GUI texture included :

 //------------------------------//
 //  Footsteps.js                //
 //  Written by Alucard Jay      //
 //  4/11/2014                   //
 //------------------------------//
 
 #pragma strict
 @script RequireComponent( AudioSource )
 
 public var walkSounds : AudioClip[];
 public var runSounds : AudioClip[];
 
 public var walkAudioSpeed : float = 0.4;
 public var runAudioSpeed : float = 0.2;
 
 private var walkAudioTimer : float = 0.0;
 private var runAudioTimer : float = 0.0;
 
 var isWalking : boolean = false; // these can be private
 var isRunning : boolean = false; // these can be private
 
 public var walkSpeed : float = 8; // regular speed
 public var runSpeed : float = 20; // run speed
 
 public var sprintDuration : float = 10.0;
 private var sprintTimer : float = 0;
 private var stamina : float = 0.0;
 
 public var staminaTexture : Texture;
 
 private var chCtrl: CharacterController;
 private var chMotor: CharacterMotor;
 
 function Start()
 {
     chCtrl = GetComponent(CharacterController);
     chMotor = GetComponent(CharacterMotor);
 }
 
 function Update()
 {
     SetSpeed();
 
     if ( chCtrl.isGrounded )
     {
         PlayFootsteps();
     }
     else
     {
         walkAudioTimer = 1000.0;
         runAudioTimer = 1000.0;
     }
 }
 
 function SetSpeed()
 {
     var speed = walkSpeed;
 
     if ( Input.GetAxis( "Horizontal" ) || Input.GetAxis( "Vertical" ) )
     {
         if ( Input.GetKey( "left shift" ) || Input.GetKey( "right shift" ) )
         {
             // Running
             isWalking = false;
             isRunning = true;
         }
         else
         {
             // Walking
             isWalking = true;
             isRunning = false;
         }
     }
     else
     {
         // Stopped
         isWalking = false;
         isRunning = false;
     }
 
     // check the sprint timer
     if ( isRunning )
     {
         sprintTimer += Time.deltaTime;
 
         if ( sprintTimer > sprintDuration )
         {
             isRunning = false;
             isWalking = true;
         }
         else if ( chCtrl.isGrounded )
         {
             speed = runSpeed;
         }
     }
     else
     {
         sprintTimer -= Time.deltaTime;
     }
     
     sprintTimer = Mathf.Clamp( sprintTimer, 0, sprintDuration );
 
     chMotor.movement.maxForwardSpeed = speed;
 
     stamina = 1.0 - ( sprintTimer / sprintDuration );
 }
 
 function PlayFootsteps()
 {
     // Play Audio
     if ( isWalking )
     {
         if ( walkAudioTimer > walkAudioSpeed )
         {
             audio.Stop();
             audio.clip = walkSounds[ Random.Range( 0, walkSounds.Length ) ];
             audio.Play();
             walkAudioTimer = 0.0;
         }
     }
     else if ( isRunning )
     {
         if ( runAudioTimer > runAudioSpeed )
         {
             audio.Stop();
             audio.clip = runSounds[ Random.Range( 0, runSounds.Length ) ];
             audio.Play();
             runAudioTimer = 0.0;
         }
     }
     else
     {
         audio.Stop();
     }
 
     // increment timers
     walkAudioTimer += Time.deltaTime;
     runAudioTimer += Time.deltaTime;
 }
 
 function OnGUI()
 {
     if ( staminaTexture ) // check there is a texture so it doesn't error if not
     {
         GUI.DrawTexture( Rect( 30, Screen.height - 80, 400 * stamina, 50 ), staminaTexture );
     }
 }




Comment
Add comment · Show 1 · 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 Entyro · Apr 11, 2014 at 08:27 AM 0
Share

Wow thanks! Works perfectly! :D I really appreciate it!

avatar image
0

Answer by VoidScreamer · Apr 10, 2014 at 09:22 AM

Create an empty game object that holds the stamina

in the update function use a damp function like Mathf.smoothdamp

smoothdamp( firrst number , target number , external vairable for speed(normally zero) , transition time)

in example : Var1 = Mathf.SmoothDamp (0 , 15 , speed , 5);

after playing , in 5 seconds , 0 reaches 15 . that means every second 3 units are added.

notice , that speed variable should be defined OUTSIDE of the Update function .

Comment
Add comment · 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

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

24 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Get Selected Object In Editor 2 Answers

C# Why Won't My GUI Layout Button Appear? 1 Answer

Gameover function calling before game ends help 2 Answers

Is it possible to link character skill lists to a GUI, and if so, how? 3 Answers


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