Move sprite up and down

How to move a sprite up and then down without any key press?

How are you rendering it now?

Use a variable for Y.Pos and modify it?

Probably…

1 Like

Use the delta time property in Update to move it however you like over time by changing the position property you pass to spritebatch.draw().

2 Likes

If you’re talking about making the sprite move constanty up and down inside an specific range maybe you could try something like this in your Update:

float yPos = 0f;
float yOrigin = 0f;
float range = 5f;
float speed = 1f;
yPos = yOrigin + range * Math.Sin(speed * 2 * Math.PI * gameTime.ElapsedGameTime.TotalSeconds);

I haven’t tested it myself but hope it helps.

1 Like