Unity3D: Simple Parallax Scrolling

Two ways to fake depth in a 2D game: UV scrolling for looping textures and object scrolling with position resets for continuous backgrounds.

Parallax scrolling is a special scrolling technique in computer graphics, creating an illusion of depth in a 2D video game. The fundamental principle involves background layers moving slower than foreground elements relative to camera movement, enhancing visual immersion.

There are two implementation approaches.

UV Scrolling Technique

UV Scrolling animates looping effects by adjusting texture coordinates based on planar position. Applications include water flows, rainfall, and background animation.

#pragma strict
var isVertical : boolean = false;
var scrollSpeed : float = 20;

function Start () {
}

function Update () {
	var offset : float = Time.time * -scrollSpeed;
	if (isVertical) {
		renderer.material.mainTextureOffset = Vector2 (0, offset);
	}else{
    	renderer.material.mainTextureOffset = Vector2 (offset, 0);
    }
}

Key variables: scrollSpeed controls movement velocity (negative values scroll left/down) and isVertical determines scroll direction orientation.

Object Scrolling Technique

This method moves objects through world space, resetting positions when they exit the camera view to create continuous looping animation.

#pragma strict

var speed : float;
var resetDistance : float;
var initialDistance : float;
var isVertical : boolean = false;

function Start () {

}

function Update ()
{
	var move : float = speed * Time.deltaTime;
	if (isVertical) {
		transform.Translate(Vector3.down * move, Space.World);
		if (transform.position.y < resetDistance)
		{
			transform.position = Vector3(transform.position.x, initialDistance, transform.position.z);
		}
	}else{
		transform.Translate(Vector3.left * move, Space.World);
		if (transform.position.x < resetDistance)
		{
			transform.position = Vector3(initialDistance, transform.position.y, transform.position.z);
		}
	}
}

Four essential variables control behavior: speed (movement velocity), isVertical (scroll orientation), initialDistance (reset position coordinate), and resetDistance (boundary trigger point).