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 NutellaDaddy · Mar 16, 2014 at 03:49 PM · guitime.deltatimedecreasestamina

I am trying to make a stamina bar and it won't drain no matter what i try.

I made a stamina bar and I want it to go down by a certain amount when you are holding shift and then go up when isRunning = false, which is set false when you lift up from the space. I can not set it to go down every second while holding the bar though, it only goes down once. Can someone please help me? Here's the script:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
 
     //SPEED AND SUCH
     public float speed = 6.0f;
     public float strafeSpeed = 5.8f;
     public float jumpSpeed = 8.0f;
     public float gravity = 18.125f;
 
     private Vector3 moveDirection = Vector3.zero;
 
     private bool grounded = false;
     private bool isRunning = false;
 
     //STAMINA BAR
     public GUIStyle staminaBar;
 
     private float maxStamina = 100.0f;
     private float curStamina = 100.0f;
     private float staminaBarLength;
 
     void Start () 
     {
         staminaBarLength = 85;
     }
     void Update () 
     {
         if (grounded) 
         {
             moveDirection = new Vector3(Input.GetAxis ("Horizontal"),0,Input.GetAxis ("Vertical"));
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= speed;
             if(Input.GetKeyDown (KeyCode.LeftShift))
             {
                 speed = 10.00f;
                 isRunning = true;
                 AdjustCurrentStamina(-1);
             }
             else if(Input.GetKeyUp (KeyCode.LeftShift))
             {
                 speed = 6.0f;
                 isRunning = false;
             }
             else if(isRunning == false)
             {
                 AdjustCurrentStamina (1);
             }
         }
         if (grounded)
         {
             if(Input.GetKey (KeyCode.Space))
             {
                 moveDirection.y = jumpSpeed;
             }
         }
         moveDirection.y -= gravity * Time.deltaTime;
             var Controller = GetComponent<CharacterController> ();
             var flags = Controller.Move (moveDirection * Time.deltaTime);
             grounded = (flags & CollisionFlags.CollidedBelow) != 0;
 
         AdjustCurrentStamina (0);
     }
     void OnGUI()
     {
         GUI.Box (new Rect (355, 476, staminaBarLength, 125), "Stamina", staminaBar);
     }
     void AdjustCurrentStamina(int adj)
     {
         
         curStamina += adj * Time.deltaTime;
         
         if(curStamina <0)
             curStamina = 0;
         
         if(curStamina > maxStamina)
             curStamina = maxStamina;
 
         if (curStamina < 100)
 
             
             if(maxStamina <1)
                 maxStamina = 1;
         
         staminaBarLength =(85) * (curStamina / (float)maxStamina);
     }
 }

Any help is appreciated!

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

1 Reply

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

Answer by Zentiu · Mar 16, 2014 at 04:07 PM

try this in your AdjustCurrentStamina method:

 if(isRunning)
 {
     curStamina -= Time.deltaTime;
 }

when you stop running (!isRunning) or (isRunning == false) it should not reduce stamina anymore.

when I did my own stamina mechanic/method I preferred this mechanic then telling it to remove a certain value.

you don't really need a value like an int and pass it into your method. besides, 2 different structs don't always work well together. it either needs to be (float (against) float) or (int (against) int).

Comment
Add comment · Show 2 · 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 Zentiu · Mar 16, 2014 at 04:09 PM 0
Share

however, if you prefer your way try this:

 //in your sta$$anonymous$$a method
 
 curSta$$anonymous$$a += (float)adj * Time.deltaTime;
avatar image NutellaDaddy · Mar 16, 2014 at 04:28 PM 0
Share

I can't thank you enough It works perfectly!

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

21 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

Related Questions

Smoothly transform gameobject to specific coordinates with the click of a GUI 1 Answer

Health bar depletes when app isn't open 1 Answer

Decrease a value every second? 3 Answers

Why does it give me an error in this small java script for sprint? 1 Answer

Adding a value to a variable every second 4 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