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

**URL:** <https://forum.makecode.com/t/how-do-i-make-a-sprite-speed-up-as-a-variable-increases/11605>\
**Category:** Help\
**Created:** [January 4, 2022, 7:26pm UTC](https://forum.makecode.com/t/how-do-i-make-a-sprite-speed-up-as-a-variable-increases/11605 "2022-01-04T19:26:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![GoMustangs](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/gomustangs/32/4323_2.png) [@GoMustangs](https://forum.makecode.com/u/GoMustangs)\
**Post date:** [January 4, 2022, 7:26pm UTC](https://forum.makecode.com/t/how-do-i-make-a-sprite-speed-up-as-a-variable-increases/11605/1 "2022-01-04T19:26:39Z")

</div>

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?

> **[FoodFinderPrototype](https://arcade.makecode.com/08682-77609-44284-64397)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [January 4, 2022, 8:30pm UTC](https://forum.makecode.com/t/how-do-i-make-a-sprite-speed-up-as-a-variable-increases/11605/2 "2022-01-04T20:30:17Z")

</div>

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:

 ![Screen Shot 2022-01-04 at 12.28.12 PM](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/2/268d5f99c79483cd1a055ba7ebc4960d4cf66541.png)

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.

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [January 4, 2022, 8:33pm UTC](https://forum.makecode.com/t/how-do-i-make-a-sprite-speed-up-as-a-variable-increases/11605/3 "2022-01-04T20:33:12Z")

</div>

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:

 ![Screen Shot 2022-01-04 at 12.31.43 PM](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/5/50df3b176dfc76b95063d37911239253f7768fa2.png)

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

---

<div class="post-metadata">

**Author:** ![GoMustangs](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/gomustangs/32/4323_2.png) [@GoMustangs](https://forum.makecode.com/u/GoMustangs)\
**Post date:** [January 4, 2022, 10:22pm UTC](https://forum.makecode.com/t/how-do-i-make-a-sprite-speed-up-as-a-variable-increases/11605/4 "2022-01-04T22:22:04Z")

</div>

Thanks! I really appreciate the help!
