Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
-1
Question by shadowpuppet · Aug 08, 2016 at 07:47 PM · sliderstamina

getkey, keyup, keydown

Posted earlier trying to fudge a stamina bar as everything I tried online is just too far off what I have to work ( and " yes" I tried VERY hard to adapt). SO my fudge is that since the left shift is the sprint, I have a script that brings down my stamina bar. I had to use "GetKey" as opposed to "GetKeyDown" because GetKeyDown only moved the bar one increment where GetKey steadily moves the bar as long as it is pressed. My problem is that I can't figure out how to reset the stamina bar. I was hoping to use "GetKeyUp" with the same key so when I release the left shift and stop running the bar would slowly replenish. But as you can see there is a conflict as I can only do the GetKeyUp once and it only replenishes one increment ( same issue I had with depleting the stamina bar). How can I reset the Stamina bar so in 5 seconds after I do "GetKeyUp" the bar will be full again? I know I could just set the amount to 100 and fill it up again but there needs to be some down time after a sprint This is the code I have for the bar

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class newStaminaTry: MonoBehaviour
 {
     public float startingStamina = 100;
     public float currentStamina;
     public Slider StaminaSlider;
 
 
     void Awake ()
     {
     
         currentStamina = startingStamina;
     }
     
     
     void Update ()
     {
         if (Input.GetKey(KeyCode.LeftShift))
         
     {
         
         currentStamina -= .5f;
         StaminaSlider.value = currentStamina;
 
     }
     
         if (Input.GetKeyUp(KeyCode.LeftShift))
             currentStamina += 10f;
         StaminaSlider.value = currentStamina;
 }
 }
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by shadowpuppet · Aug 13, 2016 at 07:21 PM

Glad to somebody is answering my desperate cries for help. Saw you responded to my nearly identical question so i thought I'd responb to this one as well. Short version I made GetKeyUp set a bool and as long as that bool was true the meter climbed again . bool = false when meter hit 100

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

Answer by Landern · Aug 08, 2016 at 08:12 PM

track when you're draining as a constraint on regaining stamina, it's a concept, implement anyway you wish:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class newStaminaTry: MonoBehaviour
 {
     public float startingStamina = 100;
     public float currentStamina;
     public Slider StaminaSlider;
 
     private bool drainStam = false;
     
     void Awake ()
     {
         currentStamina = startingStamina;
     }
 
     void Update ()
     {
         if (Input.GetKey(KeyCode.LeftShift))
         {
             drainStam = true;
             currentStamina -= .5f;
             StaminaSlider.value = currentStamina;
         } 
         else if (Input.GetKeyUp(KeyCode.LeftShift))
         {
             drainStam = false;
         }
         
         if (!drainStam && currentStamina < 100.0f)
         {
             currentStamina += 10f;
             StaminaSlider.value = currentStamina;
         }
     }
 }
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

64 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 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 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

lerp a float value of keyup 2 Answers

How do I take out a UI Slider's value? 0 Answers

How to control animation with slider 4 Answers

Slider with filter / search engine -1 Answers

Slider value help 0 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