Making a timer that ends on a consistent update

Hello! I am making a snake clone to get a better understanding of unity and coding. It uses 1 1x1 block that is the head, what you control. And a body block that is Instantiated to the head and waits to be destroyed at the right time. The head moves 1 block in any direction and will move again in a given direction after a certain amount of time. Each time it moves, it instantiates the body block to the head’s position. The snake body script immediately use Invoke(“DestroySelf” , snakeLength * snakeSpeed). This way it will destroy itself when the head has moved it’s full length. However, since it the timer might end slightly before or slightly after an update, the end of the snake will get longer or shorter then they should be. Is there a way to have the body destroy itself on a consistently so it lands on the same update away from when it was instantiated?

Hello, @GlassesGuy !

Are you sure that your time is correct for snake? If snake has higher speed, then lifetime of block should be shorter, shouldnt it? Maybe you need to devide snakeLength by snakeSpeed and multiply it by some value if needed (to increase overall lifetime)?

Also, instead of always instantiate and destroy game objects, maybe better to store a list of objects and just move last snake block to a new head position? If you are interested in it, I can explain how to do it :wink:

@Kennai , It’s multiplied because the speed is really just the amount of time it takes before it moves another block, so the smaller the speed value is the faster the snake goes. I thought it made sense to instantiate and destroy objects so there isn’t a limit since the snake can get longer based on how many apples it eats. I’m looking into tracking the amount of updates and maybe using that to trigger the timing on the snake. It would probably work the way I want but depending on what device you use the snake will be faster or slower so if you have any suggestions I would love to hear them!