Simple Parallax Scrolling for 2d games

Game Development

Parallax scrolling is a technique that is frequently used in sidescrolling 2D games to give the impression of depth. It consists of making objects that are further away appear to move slower in relation to the viewer than objects that are near.
Parallax scrolling is nothing but just different layers that all scroll at different speeds.
That’s all.
So when you move to the right the objects on layer 1 scroll by 5 pixels and the objects on layer 2 by 7 for example.
Same goes for jumping, falling down, etc but then for the Y-axis.

What you do is just create a Vector2, call it Camera1 or something.

Vector2 Camera1 = Vector2.Zero;

Then…

Add some logic so (pseudo)

if(Player moves forward in game world or player gets to a specific screen position.X)
{

Camera1 += 2; // 2 being just a random number for speed

}

Then you’ll just go into your draw code and in the draw line: spritebatch.Draw(texture, vector2 – Camera1, Color);
and subtract the Camera1 vector from your background items you want your player to pass
for parallax scrolling, you could just add another Vector2.Zero, Camera2 for example and set a different speed for it to move. Then subtract that camera from those positions.