Reputation: 23
I actually have two questions. First of all, I have simple code
public float changeTime;
public float changeTimeStart;
public bool reversed;
public float nope;
void Update(){
change ();
}
void start(){
changeTime = Time.time + changeTimeStart;
reversed = false;
}
void change(){
if (changeTime <= Time.time && reversed == false) {
reversed = true;
changeTime = Time.time + changeTime;
}
if (changeTime <= Time.time && reversed == true) {
reversed = false;
nope = 20;
}
}
Basicly in the beginning reversed is set to false.
After 3 secs first if statement happens and it sets reversed to true.
And after 3 more seconds second if statement happens and it sets the nope value to 20 but it doesn't set reversed value to false.
I can't understand why. Does the first if statement also works as soon as second statement set reversed to false? I basically want it to change every x second from false to true but I couldn't grasp the logic.
My second question is why it is 6.012766 not just 6?
Is this the time to reload the frame? Doesn't this make a problem if this cycle continues?
Upvotes: 2
Views: 124
Reputation: 236208
Simply extend change time for x seconds each time when current time exceeds change time:
void change(){
float changeRate = 3F; // x or changeTimeStart in your case
if (Time.time < changeTime)
return;
reversed = !reversed;
nope = 20;
changeTime = Time.time + changeRate; // or changeTime += changeRate
}
Your problem is doubling change time in the first case - instead of extending it by changeRate you are adding changeTime (which is a little less than the current time) with the current time. So changes will happen approx at 3, 6, 12, 24, 48 etc seconds.
And second problem is that you are not modifying changeTime in the second case. So after you change reversed
to false
on the next frame first condition hits (changeTime <= Time.time
is still true) and changes reversed
back to true
.
Upvotes: 3