Subversion Repository Public Repository

Nextrek

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
 *
 *	Adventure Creator
 *	by Chris Burton, 2013-2014
 *	
 *	"Parallax2D.cs"
 * 
 *	Attach this script to a GameObject when making a 2D game,
 *	to make it scroll as the camera moves.
 * 
 */

using UnityEngine;
using System.Collections;

public class Parallax2D : MonoBehaviour
{

	public GameCamera2D camera2D;
	public float depth;
	public bool xScroll;
	public bool yScroll;
	public float xOffset;
	public float yOffset;

	private float xStart;
	private float yStart;
	private float xDesired;
	private float yDesired;
	private MainCamera mainCamera;
	private Vector2 perspectiveOffset;


	private void Awake ()
	{
		if (GameObject.FindWithTag (Tags.mainCamera) && GameObject.FindWithTag (Tags.mainCamera).GetComponent <MainCamera>())
		{
			mainCamera = GameObject.FindWithTag (Tags.mainCamera).GetComponent <MainCamera>();
		}

		xStart = transform.localPosition.x;
		yStart = transform.localPosition.y;

		xDesired = xStart;
		yDesired = yStart;
	}


	private void Update ()
	{
		if (mainCamera)
		{
			perspectiveOffset = mainCamera.perspectiveOffset;
		}

		xDesired = xStart;
		if (xScroll)
		{
			xDesired += perspectiveOffset.x * depth;
			xDesired += xOffset;
		}

		yDesired = yStart;
		if (yScroll)
		{
			yDesired += perspectiveOffset.y * depth;
			yDesired += yOffset;
		}

		transform.localPosition = new Vector3 (xDesired, yDesired, transform.localPosition.z);
	}



}

Commits for Nextrek/SpaceCrew/SpaceCrew/Assets/AdventureCreator/Scripts/Object/Parallax2D.cs

Diff revisions: vs.
Revision Author Commited Message
83 FMMortaroli picture FMMortaroli Tue 13 May, 2014 11:32:51 +0000