Scrolling and parallax effects add depth and movement to your 2D scenes.
Use a script to move background layers at a fixed speed:
public float scrollSpeed = 0.5f;
private Vector2 offset;
void Update() {
offset = new Vector2(Time.time * scrollSpeed, 0);
GetComponent<Renderer>().material.mainTextureOffset = offset;
}
Create multiple background layers (e.g., sky, mountains, trees).
Move each layer at a different speed relative to the camera:
public Transform[] layers;
public float[] parallaxScales;
public float smoothing = 1f;
private Vector3 previousCameraPos;
void Start() {
previousCameraPos = Camera.main.transform.position;
}
void Update() {
for (int i = 0; i < layers.Length; i++) {
float parallax = (previousCameraPos.x - Camera.main.transform.position.x) * parallaxScales[i];
layers[i].position += new Vector3(parallax, 0, 0);
}
previousCameraPos = Camera.main.transform.position;
}
Activity: Add scrolling and parallax backgrounds to your 2D level for dynamic visuals.
By the end of this module, you will:
With these skills, you can create engaging 2D games that are both fun and visually compelling!