Skip to content

Instantly share code, notes, and snippets.

@nastajus
Created June 14, 2014 22:46
Show Gist options
  • Save nastajus/653f31a3e003e59f0c65 to your computer and use it in GitHub Desktop.
Save nastajus/653f31a3e003e59f0c65 to your computer and use it in GitHub Desktop.
does the compiler optimize code like declarations in loops? seems lines 6, 7 are taken care of by what Dante/Zack told me before, to not worry about. Then what about redundant if statements inside my foreach loop lines 14, 15, which are only intended for initialization only. I bet THAT is wasteful, that the compiler wouldn't remove that. The com…
void Update(){
//constantly check player positions..
//find furthest apart X, Y
//adjust camera accordingly
Vector2 min;
Vector2 max;
//ASK ZACK: what's best way to initalize this?
if ( PlayerGOs != null ) {
foreach ( GameObject PlayerGO in PlayerGOs ){
if ( min == null ) { min.x = PlayerGO.transform.position.x; min.y = PlayerGO.transform.position.y; }
if ( max == null ) { max.x = PlayerGO.transform.position.x; max.y = PlayerGO.transform.position.y; }
if ( PlayerGO.transform.position.x < min.x ) min.x = PlayerGO.transform.position.x;
if ( PlayerGO.transform.position.x > max.x ) max.x = PlayerGO.transform.position.x;
if ( PlayerGO.transform.position.y < min.y ) min.y = PlayerGO.transform.position.y;
if ( PlayerGO.transform.position.y > max.y ) max.y = PlayerGO.transform.position.y;
}
Debug.Log ( "MIN:" + min + ", MAX:" + max ) ;
}
}
void Update(){
//constantly check player positions..
//find furthest apart X, Y
//adjust camera accordingly
Vector2 min = Vector2.zero;
Vector2 max = Vector2.zero;
float biggest = 0;
//ASK ZACK: what's best way to initalize this?
if ( PlayerGOs != null ) {
foreach ( GameObject PlayerGO in PlayerGOs ){
if ( min == Vector2.zero ) min = new Vector2 (PlayerGO.transform.position.x, PlayerGO.transform.position.y );
if ( min == Vector2.zero ) max = new Vector2 (PlayerGO.transform.position.x, PlayerGO.transform.position.y );
if ( PlayerGO.transform.position.x < min.x ) min.x = PlayerGO.transform.position.x;
if ( PlayerGO.transform.position.x > max.x ) max.x = PlayerGO.transform.position.x;
if ( PlayerGO.transform.position.y < min.y ) min.y = PlayerGO.transform.position.y;
if ( PlayerGO.transform.position.y > max.y ) max.y = PlayerGO.transform.position.y;
}
float x = max.x - min.x;
float y = max.y - min.y;
biggest = x;
if ( y > x ) biggest = y;
Camera.main.orthographicSize = biggest;
Camera.main.transform.position = new Vector3 ( x/2, y/2, Camera.main.transform.position.z );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment