Parallax effect is widely used in web design, which makes two-dimensional images with three-dimensional effect. This function is going to be used on my RCA group project for a comic about Precariat.
Here is an impressive example named Protanopia which uses parallax effect well:
The C# Script to realize it mainly uses transform.position with Input.mousePosition to interact.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class parallaxEffect : MonoBehaviour {
public float Margin;
public float Layer;
float x;
float y;
float Easing = 0.2f;
Vector3 pos;
void Start () {
pos = transform.position;
}
void Update () {
float targetX = Input.mousePosition.x – Screen.width / 2f;
float dx = targetX – x;
x += dx * Easing;
float targetY = Input.mousePosition.y – Screen.height / 2f;
float dy = targetY – y;
y += dy * Easing;
Vector3 direction = new Vector3(x, y, 0f);
Vector3 depth = new Vector3(0f, 0f, Layer);
this.transform.position = pos – direction/500f * Margin + depth;
}
}