Unity tutorial: Parallax backgrounds with infinite scrolling
Begin by importing your pixel‑art assets and the player character into Unity. Configure the Cinemachine 2D virtual camera to follow the player smoothly. For each background sprite, set the Sprite Renderer filter mode to “Point (no filter)” and compression to “None.” Assign Pixels Per Unit a value of 16 to keep the pixel art crisp. Create sorting layers in the order Sky → Cloud → Background → Foreground → Player, and place the player on a layer between the background and foreground so it renders correctly.
Implementing Parallax
Add a new C# script called BackgroundController to the parent background object. Inside the script, expose a float ParallaxEffect variable. This value controls speed: 0 makes the layer move exactly with the camera (static), while 1 prevents any movement. The background’s position is calculated each frame as
startPosition + (cameraPosition * ParallaxEffect)
To avoid jitter, place the calculation in FixedUpdate rather than Update. This smooths the motion because FixedUpdate runs at a consistent physics timestep.
Adding Infinite Scrolling
Duplicate each background sprite and position the copies at the left and right edges of the original (for example, at ‑36 and +36 units). Parent the duplicates to the original background object; only the parent should hold the BackgroundController script. In the script, obtain the image length with
float length = GetComponent<SpriteRenderer>().bounds.size.x;
During each FixedUpdate, check whether the camera’s X position has moved beyond startPosition ± length. If it has, adjust startPosition by adding or subtracting the length, effectively “teleporting” the background back to the center while preserving the illusion of an endless world.
Key Settings and Values
- ParallaxEffect values: 0 (static), 0.3 (fast), 0.5 (half‑speed), 0.6 (slower), 0.8 (slow), 1 (no movement).
- Sorting layers: Sky, Cloud, Background, Foreground, Player.
- Important script notes:
- “If your Parallax effect is set to zero it’ll move with our camera; if it’s set to one it won’t move at all.”
- “Instead of using Update we want to use FixedUpdate; this will smooth out our Parallax movement.”
- “We don’t want them to have this background controller; only the parent has the background controller on it.”
Takeaways
- Set Sprite Renderer filter mode to Point (no filter), compression to None, and Pixels Per Unit to 16 for crisp pixel‑art backgrounds.
- Use a ParallaxEffect float where 0 makes a layer move with the camera and 1 keeps it stationary, applying the formula startPosition + (cameraPosition ParallaxEffect).
- Place the parallax calculation in FixedUpdate to eliminate jitter and ensure smooth motion.
- Duplicate background sprites at the edges, parent them to the original, and adjust startPosition by the image length when the camera passes the bounds to create an infinite loop.
- Organize depth with sorting layers in the order Sky, Cloud, Background, Foreground, Player, ensuring the player renders between background and foreground.
Frequently Asked Questions
How does the ParallaxEffect value affect background movement?
ParallaxEffect multiplies the camera’s X position to determine how fast a background layer moves. A value of 0 makes the layer follow the camera exactly, while 1 stops the layer from moving at all, creating depth through relative speed.
Why use FixedUpdate instead of Update for parallax motion?
FixedUpdate runs at a consistent physics timestep, preventing the frame‑rate‑dependent jitter that can occur with Update. Using FixedUpdate smooths the parallax calculation, resulting in steady background movement regardless of frame variations.
Who is Game Code Library on YouTube?
Game Code Library is a YouTube channel that publishes videos on a range of topics. Browse more summaries from this channel below.
Does this page include the full transcript of the video?
Yes, the full transcript for this video is available on this page. Click 'Show transcript' in the sidebar to read it.
Helpful resources related to this video
If you want to practice or explore the concepts discussed in the video, these commonly used tools may help.
Links may be affiliate links. We only include resources that are genuinely relevant to the topic.