Created
May 20, 2014 17:05
-
-
Save hmans/b091a58911e44d588f45 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 EndlessSpace : MonoBehaviour { | |
public GameObject tile; | |
public GameObject player; | |
private float width, height; | |
private Vector3 offset, up, right, down, left; | |
private void UpdatePositions() { | |
var origin = new Vector3( | |
Mathf.Round(player.transform.position.x / width) * width, | |
Mathf.Round(player.transform.position.y / height) * height, | |
0); | |
transform.position = origin + offset; | |
} | |
void Start () { | |
offset = transform.position; | |
width = tile.transform.localScale.x; | |
height = tile.transform.localScale.y; | |
up = height * Vector3.up; | |
right = width * Vector3.right; | |
down = -up; | |
left = -right; | |
Vector3[] positions = { Vector3.zero, up, up + right, right, | |
down + right, down, down + left, left, up + left }; | |
foreach (Vector3 position in positions) { | |
var obj = (GameObject)Instantiate(tile); | |
obj.transform.parent = this.transform; | |
obj.transform.position = position; | |
} | |
} | |
void Update () { | |
UpdatePositions(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment