Issue with Time.DeltaTime - Causing random numbers?
Hello,
I'm working on a project at the moment that requires a counter to increment once per second until it reaches a variable I can define later. In my head it makes sense however when I run the code I am getting strange results. I've read online that the timescale settings can be off so I've attached what they are, as well as the code and the outputs I'm getting.
I hope this all helps :)
if (ls_v && rs_h)
{
//outcome = "Correct";
float counter = 0f;
counter += Time.deltaTime;
Debug.Log(counter);
if (counter == 3f)
{
outcome = "Correct";
isPreaching = false;
Complete();
}
}
hello, is your code a coroutine or an asybc method? if not is hard to understand what you are oding in that method, setting counter value to 0 all the time.
Answer by theUSpopulation · Jul 16, 2021 at 02:28 PM
Hello. I found this two years too late, but I figured I would answer it in case someone else stumbles across it via Google.
Your "float count = 0;" should not be in the loop. Every time your loop occurs, it sets "count" back to zero. It should look something like this:
private float counter = 0f;
//...some code
Update(){
//...some code
if (ls_v && rs_h)
{
//outcome = "Correct";
counter += Time.deltaTime;
Debug.Log(counter);
if (counter == 3f)
{
outcome = "Correct";
isPreaching = false;
Complete();
}
}
//...some more code
}//end of Update()
Your answer

Follow this Question
Related Questions
animation script behaving differently after build and run on a different computer. 1 Answer
Does FIxedUpdate get called every 0.02 seconds? 2 Answers
Time.deltaTime creating different speeds 1 Answer
Physics inconsistent even when using FixedUpdate() or Time.DeltaTime() 0 Answers
bool not properly working and time counter only adding once 1 Answer