- Home /
Destroying Screen Wrap Ghosts That Pass Off The Left Of Screen
Hi Guys,
I am a beginner and am stuck on something I am sure is quite simple.
I have 2 scripts. One that generates endless rooms and objects. And another that vertically screen wraps the top and bottom of the screen.
The Issue is that the Generator script successfully removes objects that lass off the left hand side of the screen. But it does not destroy the screen wrapping ghosts generated in the other script.
I have tried lots of ways to destroy these ghosts once they pass off the left side of the screen but I just cannot get it to work.
Is anyone able to provide some advice on what needs to be done to solve this please?
Here are the two scripts.
Thanks.
Vertical Wrap Script:
using UnityEngine; using System.Collections;
public class VerticalWrap : MonoBehaviour {
public bool advancedWrapping = true;
Renderer[] renderers;
Transform[] ghosts = new Transform[2];
float screenWidth;
float screenHeight;
void Start()
{
renderers = GetComponentsInChildren<Renderer>();
var cam = Camera.main;
var screenBottomLeft = cam.ViewportToWorldPoint(new Vector3(0, 0,
transform.position.z));
var screenTopRight = cam.ViewportToWorldPoint(new Vector3(1, 1,
transform.position.z));
screenWidth = screenTopRight.x - screenBottomLeft.x;
screenHeight = screenTopRight.y - screenBottomLeft.y;
CreateGhostShips();
}
void Update()
{
AdvancedScreenWrap();
}
void AdvancedScreenWrap()
{
var isVisible = false;
foreach(var renderer in renderers)
{
if(renderer.isVisible)
{
isVisible = true;
break;
}
}
if(!isVisible)
{
SwapShips();
}
}
void CreateGhostShips()
{
for(int i = 0; i < 2; i++)
{
ghosts[i] = Instantiate(transform, Vector3.zero,
Quaternion.identity) as Transform;
DestroyImmediate(ghosts[i].GetComponent<VerticalWrap>());
}
PositionGhostShips();
}
void PositionGhostShips()
{
var ghostPosition = transform.position;
// Bottom
ghostPosition.x = transform.position.x;
ghostPosition.y = transform.position.y - screenHeight;
ghosts[0].position = ghostPosition;
// Top
ghostPosition.x = transform.position.x;
ghostPosition.y = transform.position.y + screenHeight;
ghosts[1].position = ghostPosition;
for(int i = 0; i < 2; i++)
{
ghosts[i].rotation = transform.rotation;
}
}
void SwapShips()
{
foreach(var ghost in ghosts)
{
if (ghost.position.x < screenWidth && ghost.position.x > -
screenWidth &&
ghost.position.y < screenHeight && ghost.position.y > -
screenHeight)
{
transform.position = ghost.position;
break;
}
PositionGhostShips();
}
}
Generator Script:
using UnityEngine; using System.Collections; using System.Collections.Generic; public class GeneratorScript : MonoBehaviour { public GameObject[] availableRooms;
public List currentRooms; private float screenWidthInPoints; public GameObject[] availableObjects; public List objects; public float objectsMinDistance = 5.0f; public float objectsMaxDistance = 10.0f; public float objectsMinY = -1.4f; public float objectsMaxY = 1.4f;
void Start () {
float height = 2.0f * Camera.main.orthographicSize;
screenWidthInPoints = height * Camera.main.aspect;
}
void AddRoom(float farhtestRoomEndX)
{
//1
int randomRoomIndex = Random.Range(0, availableRooms.Length);
//2
GameObject room = (GameObject)Instantiate(availableRooms[randomRoomIndex]);
//3
float roomWidth = room.transform.FindChild("floor").localScale.x;
//4
float roomCenter = farhtestRoomEndX + roomWidth * 0.5f;
//5
room.transform.position = new Vector3(roomCenter, 0, 0);
//6
currentRooms.Add(room);
}
void GenerateRoomIfRequired()
{
//1
List<GameObject> roomsToRemove = new List<GameObject>();
//2
bool addRooms = true;
//3
float playerX = transform.position.x;
//4
float removeRoomX = playerX - screenWidthInPoints;
//5
float addRoomX = playerX + screenWidthInPoints;
//6
float farthestRoomEndX = 0;
foreach(var room in currentRooms)
{
//7
float roomWidth = room.transform.FindChild("floor").localScale.x;
float roomStartX = room.transform.position.x - (roomWidth * 0.5f);
float roomEndX = roomStartX + roomWidth;
//8
if (roomStartX > addRoomX)
addRooms = false;
//9
if (roomEndX < removeRoomX)
roomsToRemove.Add(room);
//10
farthestRoomEndX = Mathf.Max(farthestRoomEndX, roomEndX);
}
//11
foreach(var room in roomsToRemove)
{
currentRooms.Remove(room);
Destroy(room);
}
//12
if (addRooms)
AddRoom(farthestRoomEndX);
}
void AddObject(float lastObjectX)
{
//1
int randomIndex = Random.Range(0, availableObjects.Length);
//2
GameObject obj = (GameObject)Instantiate(availableObjects[randomIndex]);
//3
float objectPositionX = lastObjectX + Random.Range(objectsMinDistance, objectsMaxDistance);
float randomY = Random.Range(objectsMinY, objectsMaxY);
obj.transform.position = new Vector3(objectPositionX,randomY,0);
//int randomIndex2 = Random.Range(-1, 2);
//GetComponent<Rigidbody2D>().velocity = Vector2.up * randomIndex2;
//gameObject.GetComponent
//4
//float rotation = Random.Range(objectsMinRotation, objectsMaxRotation);
//obj.transform.position = Quaternion.Euler(Vector2.up * randomIndex2);
//5
objects.Add(obj);
}
void GenerateObjectsIfRequired()
{
//1
float playerX = transform.position.x;
float removeObjectsX = playerX - screenWidthInPoints;
float addObjectX = playerX + screenWidthInPoints;
float farthestObjectX = 0;
//2
List<GameObject> objectsToRemove = new List<GameObject>();
foreach (var obj in objects)
{
//3
float objX = obj.transform.position.x;
//4
farthestObjectX = Mathf.Max(farthestObjectX, objX);
//5
if (objX < removeObjectsX)
objectsToRemove.Add(obj);
}
//6
foreach (var obj in objectsToRemove)
{
objects.Remove(obj);
Destroy(obj);
}
//7
if (farthestObjectX < addObjectX)
AddObject(farthestObjectX);
}
// Update is called once per frame
void FixedUpdate ()
{
GenerateRoomIfRequired();
GenerateObjectsIfRequired();
}
}
Your answer
Follow this Question
Related Questions
2D C# destroy a GameObject on collision 2 Answers
Distribute terrain in zones 3 Answers
destroying gameobject from other script 1 Answer
How to destroy Instantiated objects (C#)(not last instantiated one) 0 Answers
Increase score upon an enemy's death 2 Answers