- Home /
time.Deltatime not counting Upwards
This script is suppose to make a loop of a cube moving back and forth.
var end : float = 4.0;
var Move : int = 8;
function Update () {
if(end <= 4)
{
Move = 8;
transform.Translate(0, 0, Move * Time.deltaTime);
end -= Time.deltaTime;
if(end <= 0)
{
end += Time.deltaTime;
Move = -16;
transform.Translate(0, 0, Move * Time.deltaTime);
}
}
}
But the timer doesn't go back up. It doesn't stay at zero either, which is weird. the variable Move is set equal to -16 and then the variable end locks in place around 0.005373292, but doesn't rise back up to 4 to continue the loop. The cube then infinitely goes backwards. Is their a specific way to make Time.deltaTime go up once its already going down?
Answer by xt-xylophone · Mar 06, 2014 at 04:04 AM
The logic of your code is the problem. The outer if statement is lowering it as long as it is
What you want is a cube to go back and forward over 4 seconds? Try using an InvokeRepeating as well as a boolean variable to keep track of which direction you want. The InvokeRepeating can keep swapping the bool and the cube moves based on the bool. Check the Unity docs for how InvokeRepeating works.
Your answer
Follow this Question
Related Questions
What is the reasoning of multiplying by Time.deltaTime 1 Answer
Simple Transform Move 3 Answers
transform.Translate by Time.deltaTime 2 Answers
MoveTowards happening instantly 1 Answer