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
0
Question by shadowpuppet · Aug 09, 2016 at 03:48 AM · floatsliderstamina

lerp a float value of keyup

I have a stamina slider bar for when the character is sprinting. I used GetKey so that as long as I have the LeftSHift key pressed the value and the slider goes down. That works fine. the problem is that I need to get the value back up when I release the LeftShift key. I already used the GetKey to make it go down so I have to use GetKeyUp. so it only goes up once . I could set that to = 100 and the stamina bar would be full again but it happens immediately. I need it to move from current value to 100 over say 7 seconds.here is my script with the stamina bar filling back up immediately. Can anyone help me please. I have been at this for two days to get what I have here by adapting my healthslider script and now I am stuck.I even tried having the getkeyup enable a box collider attached to the player with an onTriggerStay ( to emulate a getkey constant press) to slowly raise the value but was unable to access the stamina script - wasting 4 hours of my day

 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) && Input.GetKey(KeyCode.W))
         {
 
             currentStamina -= .5f;
             StaminaSlider.value = currentStamina;
 
         }
         if (currentStamina< 0)
         {
             currentStamina= 0;
         }
     
         if (Input.GetKeyUp(KeyCode.LeftShift))
         {
             currentStamina =100f;
 
 
             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
0
Best Answer

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

thanks for the reply but I realized what I was trying to do was ineffective. lerping the timer back up in no way was tied to the players actual ability to sprint and was just a visual thing. So , among other changes, I made it so releasing the LeftShift key set a bool "cantRun" = true. and had that a condition for the timer. So just as when the LeftShift was still down the meter went down( canRun = false) as long as cantRun is true the meter would rise again. Basically GetKEy moved the meter down, GetKeyUp set a bool making the meter rise again. but , of course added other code to actually tie the meter to the players actual ability to sprint or not. when the meter hits zero or I release the LeftShift the animation sprint stops and blends to regular movement. this is just the script for meter going up. there are actually two other scripts that go along with it . I tried combing them but it just kept breaking so I left them in three parts.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class replenishStamina : MonoBehaviour {
     private MonoBehaviour  canSprint;
     public float currentStamina;
     public  Slider StaminaSlider;
     public bool rePlenishing = false;
 
 
     void Awake ()
     {
          
 
         currentStamina = 100;
         canSprint =  GetComponent<sprintScript>();
     }
     
     void Update ()
 
     {
 
         if (Input.GetKeyUp(KeyCode.LeftShift))
         {
             
             rePlenishing = true;
             
         } 
         if (Input.GetKeyDown(KeyCode.LeftShift))
         {
             
             rePlenishing = false;
             
         } 
         if (rePlenishing == true)
 
             newStaminaTry.currentStamina +=.5f;
         StaminaSlider.value = newStaminaTry.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
avatar image
0

Answer by 5c4r3cr0w · Aug 10, 2016 at 10:02 AM

You can start coroutine when you release the left shift.

So you need to write something like :

          if (Input.GetKeyUp(KeyCode.LeftShift))
          {
        
              StartCoroutine(fillSlider(currentStamina)); 
    
          }


Now you need to move it like 7 secodns so lets take it in consideration

 IEnumerator fillSlider (float currentStamina)
 {
     float t = 0;
     float initStamina = currentStamina;

     while (currentStamina < 100) {
         t += Time.deltaTime;
         currentStamina = Mathf.Lerp (initStamina, 100, t * 0.142857f);  // thats value of 1/7 ;)
         yield return null;
     }

 }
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

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

Related Questions

How to move an object's own Y position relative to a slider's Y position? 1 Answer

getkey, keyup, keydown 2 Answers

Hunger Script Help 1 Answer

[SOLVED!] PlayerPrefabs SetFloat and GetFloat is not working 0 Answers

How to assign slider to float: Spin, on a gameobject 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