I am trying to make a system where the player’s speed gradually increases. I found a way to do this using the fixed update function, however that updates too fast. If there is another way to do this that a beginning developer might understand, then please show me. Thanks for you help!
That while loop is simply going to repeat over and over within a single frame, thus never getting to the rest of the code and thus never getting to the next frame. It is very important that when using a while loop that something WITHIN the loop will cause the condition to be false and thus exit the loop.
Try this:
[SerializeField]
private float speedIncreasePerSecond;
private float speed;
private bool moving;
void Update()
{
if(moving)
speed += speedIncreasePerSecond * Time.deltaTime;
}
As stated above, you’ve created what is known as an infinite loop and because all these scripts run on the main-thread only, it will lock the engine up (not “crash” it).
Also, a minor thing, but there’s no such thing as Unity 2D.
private void Start()
{
constMove = true;
}
private void Update()
{
if(constMove == true)
{
// if you want it to run a check non-stop
Move();
}
// Pretend the above if statement doesn't exist
if(constMove == true)
{
constMove = false;
// Now your code will only run once.
// At the end of your Move function, you would make
// constMove true again if you wanted it to be able to run again
Move();
}
}