hi, I’m trying to create a space shooter. that means, that the playerObject is able to move on the x and the z axis. therefor I want to create an infinite background. I saw a lot of exampels for a infinite horizontal but not one for a horizontal and vertical infinite background. how can I realize this?!
my code (see below) is not working - how can i change it?! thank’s in foreward!
using UnityEngine;
using System.Collections;
public class CreateBackgroundContunius : MonoBehaviour {
public GameObject hazard;
private Camera cam;
private bool hasARightBuddy = false;
private bool hasALeftBuddy = false;
private bool hasAUpperBuddy = false;
private bool hasALowerBuddy = false;
private float backGroundWidth;
private float backGroundHeight;
private Transform myTransform;
void Awake(){
cam = Camera.main;
myTransform = transform;
}
void Start(){
backGroundWidth = renderer.bounds.size.x;
backGroundHeight = renderer.bounds.size.z;
}
void Update(){
float camHorizontalExtends = cam.orthographicSize * Screen.width / Screen.height;
float camVerticalExtends = cam.orthographicSize * Screen.height / Screen.width;
float edgeVisiblePositionRight = myTransform.position.x + backGroundWidth / 2 - camHorizontalExtends;
float edgeVisiblePositionLeft = myTransform.position.x - backGroundWidth / 2 + camHorizontalExtends;
float edgeVisiblePositionHeigh = myTransform.position.z + backGroundHeight / 2 - camVerticalExtends;
float edgeVisiblePositionLow = myTransform.position.z - backGroundHeight / 2 + camVerticalExtends;
if(cam.transform.position.x >= edgeVisiblePositionRight && hasARightBuddy == false){
spawnBackgroundLeftOrRight(1);
hasARightBuddy = true;
}else if(cam.transform.position.x <= edgeVisiblePositionLeft && hasALeftBuddy == false){
spawnBackgroundLeftOrRight (-1);
hasALeftBuddy = true;
}
if (cam.transform.position.z >= edgeVisiblePositionHeigh && hasAUpperBuddy == false) {
spawnBackgroundUpOrDown(1);
hasAUpperBuddy = true;
}else if(cam.transform.position.z <= edgeVisiblePositionLow && hasALowerBuddy == false){
spawnBackgroundUpOrDown(-1);
hasALowerBuddy = true;
}
}
void spawnBackgroundLeftOrRight(int leftOrRight){
Vector3 spawnPosition = new Vector3 (myTransform.position.x + backGroundWidth * leftOrRight, myTransform.position.y, myTransform.position.z);
Transform newBuddy = Instantiate (myTransform, spawnPosition, Quaternion.Euler (90.0f,0.0f,0.0f)) as Transform;
float spawnRangeL = spawnPosition.x - backGroundWidth/2;
float spawnRangeR = spawnPosition.x + backGroundWidth/2;
float spanwRangeU = spawnPosition.z + backGroundHeight / 2;
float spawnRangeB = spawnPosition.z - backGroundHeight / 2;
for (int i = 0; i<10; i++) {
Instantiate (hazard, new Vector3 (Random.Range (spawnRangeL, spawnRangeR), 0.0f, Random.Range (spanwRangeU, spawnRangeB)), Quaternion.identity);
}
newBuddy.parent = myTransform.parent;
if (leftOrRight > 0) {
newBuddy.GetComponent<CreateBackgroundContunius> ().hasALeftBuddy = true;
} else {
newBuddy.GetComponent<CreateBackgroundContunius>().hasARightBuddy = true;
}
}
void spawnBackgroundUpOrDown(int upperOrLower){
Vector3 spawnPosition = new Vector3 (myTransform.position.x,myTransform.position.y,myTransform.position.z + backGroundHeight * upperOrLower);
Transform newBuddy = Instantiate (myTransform, spawnPosition, Quaternion.Euler (90.0f,0.0f,0.0f)) as Transform;
newBuddy.parent = myTransform.parent;
if (upperOrLower > 0) {
newBuddy.GetComponent<CreateBackgroundContunius> ().hasALowerBuddy = true;
} else {
newBuddy.GetComponent<CreateBackgroundContunius>().hasAUpperBuddy = true;
}
}
}