Created
August 23, 2011 05:59
-
-
Save boj/1164450 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class ParallaxScroll : MonoBehaviour { | |
public float furthestDepth = 100.0F; | |
public float depthStep = 10.0F; | |
public float baseSpeed = 0.001F; | |
public int baseSpeedup = 2; | |
public GameObject[] parallaxLayerPrefabs = new GameObject[2]; | |
private GameObject[] parallaxLayers = new GameObject[2]; | |
private Transform lastCameraPosition; | |
void Awake() { | |
for (int i = 0; i < parallaxLayerPrefabs.Length; i++) { | |
parallaxLayers[i] = Instantiate(parallaxLayerPrefabs[i], new Vector3(0.0F, 0.0F, furthestDepth), Quaternion.identity) as GameObject; | |
furthestDepth -= depthStep; | |
} | |
lastCameraPosition = Camera.main.transform; | |
} | |
void Update() { | |
float distance = Vector3.Distance(transform.position, lastCameraPosition.position); | |
if (distance == 0.0F) return; | |
float speed = baseSpeed; | |
for (int i = 0; i < parallaxLayers.Length; i++) { | |
parallaxLayers[i].transform.position = new Vector3(lastCameraPosition.position.x * (distance * speed), lastCameraPosition.position.y * (distance * speed), parallaxLayers[i].transform.position.z); | |
speed *= baseSpeedup; // speed increases the closer to the camera the layer gets | |
} | |
lastCameraPosition = Camera.main.transform; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment