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;
}
}
}
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;
}
}
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;
}
}