- Home /
Time.time button delay trick causing NaN
EDIT-I solved this, the processor was receiving extra lines of code performing transform positions etc while a time delay variable was waiting for Time.time to catch up, the actual code that was supposed to hold was being ignored, I don't know why, but I found the solution was setting Time.time to 0.
hello, I have a number of key presses to Dig tunnels, and I have made a small code to make the digger dig automatically with emulated key presses- it all works fine except in the case where I have narrow tunnels only one block wide, where there is an error related to time delay:
If the lastFire key-delay variable is very small likes 0.1 seconds, the digger jumps by 100 spaces at a time and flies off into space like -7000 in a couple of seconds, and it completely ignores a)the last fire timing restriction and b) it multiplies the next space calculation by a lot.
If the lastFire key-delay variable is very larger likes 0.5 seconds, it jumps by 1000 spaces at a time and flies off into space like -800,000/Infinity/NaN in a couple of seconds, and it ignores the last fire timing restriction, it is clearly digging dozens of space is a second instead of two, and jumping to 1000 metres away instead of just 1.
If it digs narrow tunnels one block wide and uses keyboard keys instead of automatic dig navigation variable, no errors.
Here's the code-
function Update ()
{
if ( Input.GetKeyDown("x")){//increment x and flip on /off with %2
automaticDigging += 1;
pressedKeys = 0;
}
if ( automaticDigging%2== 1){//automatic digging 0/1 is on
print( " rndc" + rndc );
if (pressedKeys % randomSteps == 0)//sets random number of key presses in succession to press one key for and when it has completed that number, chooses another random number(randomSteps ) and a random key to press (randomKey)
{randomSteps = Random.Range (2, 10);
randomKey = Random.Range (1, 7);//choose one of six directions
}
}
else{rndc=0;}//automatic digging is off
//if key is pressed or a key emulator variable (randomKey)found:
if ( (Input.GetKey("l") || randomKey == 1 )&& Wt==1 && Time.time > lastFire)
{
lastFire = Time.time + fireRate;
mov = stp1;
DigNavigate (tree,1,burrowLength);
m=m+1; pressedKeys = pressedKeys + 1;
}
if ( (Input.GetKey("j") || randomKey == 2...// etc same for other digging keys ijkl
The larger tunnels dig automatically fine because they take more processing time and it prevents the above loop from interrupting itself or messng up the Time.time variable.
the if condition seems to be ignoring the Time.time restriction and putting extra lines of commands into the processor while it waits for Time.time to complete, I found the solution! Just putting Time-delay between keys to 0 makes it runs fine, when the key is emulated by a variable.