Detecting edge of screen, and then turning around.

I am simply testing the water here, as have always used Clamp to maintain enemies, and players to window. However, with the window size being able to change, I’d need a more fluid system.

Ideally, what I’d want to do is attach this script to an enemy, whom would walk off, and when hitting the screen edge, turn around.

Here’s what I thought would work…

function Update () {	

	transform.Translate(0, Time.deltaTime*2, 0);	

    var pos : Vector3 = camera.WorldToScreenPoint (transform.position);
    
    if(pos.x < 0.0){
    transform.eulerAngles = Vector3(0,180,0);
    }
    
    if(1.0 < pos.x){
   	transform.eulerAngles = Vector3(0,180,0);
    }
    
    if(pos.y < 0.0){ 
 	transform.eulerAngles = Vector3(0,180,0);
    }
    
    if(1.0 < pos.y){
	 transform.eulerAngles = Vector3(0,180,0);
    }
    
}

Any ideas are welcome.

Thank you

what you are working about now is correct, simply use WorldToViewportPoint, cause WorldToScreenPoint is screen-dependent, and you should compare it with 0…width/height range in pixels. ViewPort is always 0…1 range