Skip to main content

Posts

Showing posts from May, 2014

Bounds.intersect is bad, mkay?!

I was trying to check when enemy ships are getting out of screen to destroy objects (optimization). First version Vector2 screenSize = new Vector3 (Screen.width, Screen.height, Mathf.Abs (Camera.main.transform.position.z)); Bounds screenBounds = new Bounds (new Vector2(0f,0f), screenSize); Debug.Log ("Ship " + ship.renderer.bounds + " Screen " + screenBounds); if (!ship.renderer.bounds.Intersects (screenBounds)) ship.Destroy (); So apparently this operation is taking significant time to check, end up replacing with this Vector3 screenPos = Camera.main.WorldToScreenPoint( ship.transform.position ); Vector3 screenSize = Camera.main.WorldToScreenPoint( ship.renderer.bounds.size); if (screenPos.x + screenSize.x < 0) ship.Destroy ();