How do I make a sprite speed up as a variable increases?

I have a game where the player eats food. If they eat the apple or the strawberry they get a point, and the location of the apple and strawberry changes when they eat two of them. The player needs to dodge the doughnut while doing this.

I want the doughnut to move faster once the player has eaten a certain amount of apples and strawberries, but my game keeps glitching out an pushing the doughnut into the corner when it speeds up.

Any suggestions?

In your example you’re setting the velocity to vx=100 and vy=100 every frame which means it’ll always be going towards the bottom right.

If you want it to speed up, you need to take the angle of the velocity into account. You can do this with a little trig:

In this example, the atan2 gives us the angle of the velocity in radians. Then you multiply the x speed by the cos and the y speed by the sin.

1 Like

Alternatively, you can avoid the trig by moving your speed up code into the overlap event. That way it will only run once each overlap instead of once every frame:

Every time it runs the angle of velocity will be reset to 45 degrees however, since the vx and vy are always the same.

1 Like

Thanks! I really appreciate the help!