Skip to main content

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

Comments

devidnayana said…


I am very happy when read this blog post because blog post written in good manner and write on
good topic. Thanks for sharing valuable information.

School Information Management System,
Online Fee Payment Integration
College Management System

Message