class CP1BulletController : ZilchComponent { //This script is applied to the bullet (or other projectile) object. Usually to the archetype as bullets are //spawned over time in the game. Ensure that you use the naming convention in the script for your game objects, //or change them to match your game objects. //Store a reference object for reccuring use var LevelObject : Cog = null; function Initialize(init : CogInitializer) { // Find the LevelSetting Object for the level the object is in this.LevelObject = this.Space.FindObjectByName("LevelSettings"); //Connect to Logic Update to check screen boundaries Zero.Connect(this.Space, Events.LogicUpdate, this.OnLogicUpdate); } function OnLogicUpdate(updateEvent : UpdateEvent) { // If the bullet is off screen if(this.BoundaryChecks()) { // Destroy the bullet Console.WriteLine("Destroy Bullet."); this.Owner.Destroy(); } } // Checks if the object is not in the screen. Returns true if offscreen function BoundaryChecks() : Boolean { // Step 1: Determine the screen boundaries in world space // Find out Screen size from Camera Viewport var camViewport = this.LevelObject.CameraViewport; // Get the width and height of the screen at a depth of 0.0 since it's 2D: var screenHalfExtents = camViewport.ViewPlaneSize(0.0) * 0.5; var camPos = camViewport.Camera.Transform.Translation; // Knowing the camera is in the middle add or subtract half the // size from the camera's position to find the borders: var leftBoundary = camPos.X - screenHalfExtents.X; var rightBoundary = camPos.X + screenHalfExtents.X; var topBoundary = camPos.Y + screenHalfExtents.Y; var bottomBoundary = camPos.Y - screenHalfExtents.Y; // Step 2: Check against the boundaries // Make the boundary checks: var pos = this.Owner.Transform.Translation; // If too far left, if (pos.X < leftBoundary) { Console.WriteLine("Bullet off left screen edge."); return true; } // If too far right else if( pos.X > rightBoundary) { Console.WriteLine("Bullet off right screen edge."); return true; } // If too far up, if (pos.Y > topBoundary) { Console.WriteLine("Bullet off top screen edge."); return true; } // If too far down else if( pos.Y < bottomBoundary) { Console.WriteLine("Bullet off bottom screen edge."); return true; } return false; } }