• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by BlueMoon22 · May 19, 2015 at 07:34 PM · c#2ddestroyoffscreen

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();
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Simple on Collision Help (C#) 2 Answers

I can't get 2DCollider to work. 1 Answer

Script won't destroy prefab clones... 1 Answer

2D C# destroy a GameObject on collision 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges