- Home /
While something is reoccuring keep value the same
Hello,
Been playing in Unity for about a week now. So far I was able to find all the answers all around the internet, but for this problem I'm clueless (mainly, because I don't know how to properly formulate the question...).
In this game if you click the character fox it sets a parameter to certain value, then it should wait for 2 seconds and revert the parameter.
void FixedUpdate(){
....
if (hit.transform.name == "fox")
AudioRecorder.noiseMin = 20;
else
StartCoroutine(ReturnNoiseMin());
IEnumerator ReturnNoiseMin()
{
yield return new WaitForSeconds(2f);
AudioRecorder.noiseMin = 0.5f;
}
...
}
Works fine when you click the character only 1. However when you keep clicking, the first 2 seconds pass and for a few frames it sets the value back. Which is kind of logical and expected, but I can't figure out how to "prolong" this 2 seconds with each click.
I tried using Time.time + 2 seconds, but its the same thing just written differently...
I believe the answer is simple and stupid, but I just can't figure it out and this is my second game with the same issue. In the first it was on collision => slow down object => after 0.5 seconds return speed, but if you kept colliding every 0.5 seconds you got the speed back for few frames...
Thank you very much for any hint. Best Regards Petr
Answer by berkun5 · Aug 11, 2021 at 10:13 AM
void FixedUpdate(){
....
if (hit.transform.name == "fox")
AudioRecorder.noiseMin = 20;
else
StartCoroutine(ReturnNoiseMin());
IEnumerator ReturnNoiseMin()
{
yield return new WaitForSeconds(2f);
AudioRecorder.noiseMin = 0.5f;
}
...
}
Hi @djpitris , In this script, you are settings the AudioRecorder.noiseMin to 20 if the name is Fox. So if the name is actually Fox it never runs the else part and never starts the coroutine.
If i understood correct, you want to set the AudioRecorder.noiseMin = 20, wait for 2 seconds, set the AudioRecorder.noiseMin = 0.5f.
You can go for something like this:
bool isActive;
void FixedUpdate()
{
if (hit.transform.name == "fox" && !isActive)
{
AudioRecorder.noiseMin = 20;
StartCoroutine(ReturnNoiseMin());
}
else
{
return;
}
}
IEnumerator ReturnNoiseMin()
{
isActive = true;
yield return new WaitForSeconds(2f);
AudioRecorder.noiseMin = 0.5f;
isActive = false;
}
Answer by djpitris · Aug 11, 2021 at 10:35 AM
Hi @berkun5 ,
Thank you very much for your response and hint.
After I wrote it I realized the mistake and it seems I also found a different solution?
I set slappedTime to the time the touch happened and outside the hit detection I check if the time is lower than current time - 2 seconds, then run the ReturnNoiseMin.
Time to fix my previous game too :-)
void FixedUpdate()
{
if (slappedTime <= Time.time - 2f && AudioRecorder.noiseMin != 0.5f)
{
ReturnNoiseMin();
}
if (hit.transform.name == "fox")
{
AudioRecorder.noiseMin = 20;
slappedTime = Time.time;
}
void ReturnNoiseMin()
{
AudioRecorder.noiseMin = 0.5f;
}
Works on my other game too, so I hope its not too wrong, I will keep it here for others.
In the other game I check if I'm colliding with certain thing (I leaved out the unrelated IFs...)
void FixedUpdate()
{
...
if (digTime <= Time.time - 0.25f && speed != 8f)
{
SetBackSpeed();
}
...
}
private void OnCollisionStay2D(Collision2D other)
{
...
digTime = Time.time;
speed = 4f;
...
}
private void SetBackSpeed()
{
speed = 8f;
}
Your answer
Follow this Question
Related Questions
Sequentially wait for multiple coroutines to finish 0 Answers
Have a function to Wait until true? 4 Answers
Help With "For Loop" Not Working? 2 Answers
Find Position in Array or List? 2 Answers