-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tiling.cs
84 lines (61 loc) · 2.64 KB
/
Tiling.cs
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
76
77
78
79
80
81
82
83
84
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(SpriteRenderer))]
public class Tiling : MonoBehaviour {
public int offsetX = 2;
public bool hasARightBuddy = false;
public bool hasALeftBuddy = false;
public bool reverseScale = false;
private float spriteWidth = 0f;
private Camera cam;
private Transform myTransform;
void Awake()
{
cam = Camera.main;
myTransform = transform;
}
void Start ()
{
SpriteRenderer sRenderer = GetComponent<SpriteRenderer>();
spriteWidth = sRenderer.sprite.bounds.size.x;
}
void Update ()
{
if (hasALeftBuddy == false || hasARightBuddy == false)
{
//calculate the camera's extend( half the width) of what the camera can see in world coordinates
float camHorizontalExtend = cam.orthographicSize * Screen.width / Screen.height;
//we are going to calculate the x position, where the camera can see the edge of the sprite(element)
float edgeVisiblePositionRight = (myTransform.position.x + spriteWidth / 2) - camHorizontalExtend; //factoring in our own positon ,adding half the sprite widht(spriteextend) and subtracting the camhorizontal extend to figure out the position where we would intersect
float edgeVisiblePositionLeft = (myTransform.position.x + spriteWidth / 2) + camHorizontalExtend;
if (cam.transform.position.x >= edgeVisiblePositionRight - offsetX && hasARightBuddy == false)
{
MakeNewbuddy(1);
hasARightBuddy = true;
}
else if (cam.transform.position.x <= edgeVisiblePositionLeft - offsetX && hasALeftBuddy == false)
{
MakeNewbuddy(-1);
hasALeftBuddy = true;
}
}
}
void MakeNewbuddy(int rightOrLeft)
{
Vector3 newPosition = new Vector3 (myTransform.position.x + spriteWidth * rightOrLeft, myTransform.position.y, myTransform.position.z);
Transform newBuddy = Instantiate(myTransform, newPosition, myTransform.rotation) as Transform;
if (reverseScale == true)
{
newBuddy.localScale = new Vector3(newBuddy.localScale.x * -1, newBuddy.localScale.y, newBuddy.localScale.z);
}
newBuddy.parent = myTransform.parent;
if (rightOrLeft > 0)
{
newBuddy.GetComponent<Tiling>().hasALeftBuddy = true;
}
else
{
newBuddy.GetComponent<Tiling>().hasARightBuddy = true;
}
}
}