- Home /
InvokeRepeating help
Hey everyone, i have a problem with InvokeRepeating and variables. I have a float called hunger, and im using an InvokeRepeating to make sure the hunger float decreases every x seconds. This works fine, but what i want is for it to decrease even faster if i hold down a button. Therefor, i obviously use variables, which you can see in my code below. When i press said button, i can see the variables change in the inspector, but the hunger float is actually not decreasing any faster. Is this some sort of limitation w/ InvokeRepeating? Or is something wrong with my code?
C#:
public TextMeshProUGUI hungerText;
public float hunger;
public float hungerDelay;
public float hungerRate;
// Use this for initialization
void Start () {
//hungerDelay = 0.5f;
hunger = 100;
InvokeRepeating ("HungerDecay", hungerDelay, hungerRate);
}
void Update () {
hungerText.text = "" + hunger;
//hungerSpeed -= 0.1f;
if (Input.GetKey (KeyCode.LeftShift)) {
hungerRate = 0.0001f;
}
void HungerDecay()
{
hunger -= 1;
}
}
Once the value is passed to invoke, it's done. You can try coroutine loop and change its wait time - BUT the new time will be applied on the next "cycle" (i.e once current wait starts it is unchangeable similar to invoke, the smaller wait value will be applied for the next wait period). If you want it completely responsive then maybe do your own ti$$anonymous$$g in Update(). Probably something like hungerTimer += Time.deltaTime * speedCoefficient , with the coefficient being 1 on released button and some other value on pressed button. Then when timer reaches X change hunger by Y and reset hungerTimer to 0.
Answer by Prastiwar · Dec 16, 2017 at 08:00 PM
You need to CancelInvoke and set it again after changing variable.
if (Input.GetKey(KeyCode.LeftShift))
{
if (hungerRate == yourBaseRate) // to make sure instruction will happen only once
{
CancelInvoke("HungerDecay");
hungerRate = 0.0001f;
InvokeRepeating("HungerDecay", hungerDelay, hungerRate);
}
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
CancelInvoke("HungerDecay");
hungerRate = yourBaseRate; // so you can set to "not holding" variable.
InvokeRepeating("HungerDecay", hungerDelay, hungerRate);
}
Took me a while to understand it lol but your answer absolutely works! Thanks a lot :)
Wouldn't this generate garbage and reset the repeat timer on each shift press/release?
Answer by vardevelopment · Dec 16, 2017 at 08:00 PM
I would do something like this:
public TextMeshProUGUI hungerText;
public float hunger;
public float hungerDelay;
public float hungerRate;
public float originalHungerRate;
// Use this for initialization
void Start () {
//hungerDelay = 0.5f;
originalHungerRate = hungerRate;
hunger = 100;
InvokeRepeating ("HungerDecay", hungerDelay, hungerRate);
}
void Update () {
hungerText.text = "" + hunger;
//hungerSpeed -= 0.1f;
if (Input.GetKey (KeyCode.LeftShift)) {
CancelInvoke();
hungerRate = 0.0001f;
InvokeRepeating ("HungerDecay", hungerDelay, hungerRate);
} else if (Input.GetKeyUp (KeyCode.LeftShift)) {
CancelInvoke();
hungerRate = originalHungerRate;
InvokeRepeating ("HungerDecay", hungerDelay, hungerRate);
}
void HungerDecay()
{
hunger -= 1;
}
}
I don't know entirely if this works or not but maybe it will.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
random respawn and respawn delay 1 Answer
Problem with swithing weapons in a list of scriptable objects 1 Answer
Accessing variables from other scripts 0 Answers
Initilization of variables in shader 1 Answer