Created
July 14, 2012 11:13
-
-
Save orange030/3110653 to your computer and use it in GitHub Desktop.
Unity 2D Parallax Layer
This file contains hidden or 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; | |
public class ParallaxLayer : MonoBehaviour | |
{ | |
public Transform originalPosition; | |
public Transform layerPosition; | |
public float _xFator; | |
public float _yFator; | |
public float _xMaxOfset; | |
public float _xMaxNegativeOfset; | |
public float _yMaxOfset; | |
public float _yMaxNegativeOfset; | |
void Update() | |
{ | |
var lCameraPosition = Camera.main.transform.position; | |
var lPosition = originalPosition.position; | |
var lToCamera = new Vector2(lCameraPosition.x - lPosition.x, lCameraPosition.y - lPosition.y); | |
var lX = Mathf.Clamp(lToCamera.x * _xFator, _xMaxNegativeOfset, _xMaxOfset); | |
var lY = Mathf.Clamp(lToCamera.y * _yFator, _yMaxNegativeOfset, _yMaxOfset); | |
var lLayerPosition = layerPosition.position; | |
layerPosition.position = new Vector3(lPosition.x + lX, lPosition.y + lY, lLayerPosition.z); | |
} | |
void OnDisable() | |
{ | |
if (layerPosition && originalPosition) | |
{ | |
var lPosition = originalPosition.position; | |
var lLayerPosition = layerPosition.position; | |
layerPosition.position = new Vector3(lPosition.x, lPosition.y, lLayerPosition.z); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment