c# - Fixing the camera's left side in a 2D Game in all screen resolutions -
i started making 2d game using unity , have resolution & cam problem. have 2d terrain , i'm trying prepare left side of cam left side of terrain. however, when alter screen resolution iphone 4 example, left side hidden. tried script cam it's not working :
void awake() { ray ray = camera.main.screenpointtoray(new vector3(screen.width/ 2, screen.height / 2,-10)); camera.main.orthographicsize = ray.origin.y; transform.position = new vector3 (ray.origin.x, ray.origin.y,-10); }
you want utilize camera.pixelrect (also see camera.pixelwidth , camera.pixelheight).
you're finding world point located @ center of screen in world coordinates, changing camera's viewport size based on that, , setting camera's position center of screen. if want anchor viewport's left border x = 0 (assuming terrain's left border @ x = 0) should find viewport's xmax, convert screen coordinates world coordinates, , set camera's x position that.
void awake() { // set camera's orthographic size. // camera's pixelwidth, pixelheight, , pixelrect relative it. vector3 worldcenter = camera.main.screentoworldpoint (new vector3 (screen.width / 2, screen.height / 2, -10)); camera.main.orthographicsize = worldcenter.y; // viewport's rectangle , calculate camera's desired position. // want utilize viewportrect.xmax since want move photographic camera right half of view width. rect viewportrect = camera.main.pixelrect; vector3 cameraposition = new vector3 (viewportrect.xmax, 0, 0); cameraposition = camera.main.screentoworldpoint (cameraposition); cameraposition.z = -10; transform.position = cameraposition; }
some references:
http://docs.unity3d.com/scriptreference/camera.html
http://docs.unity3d.com/scriptreference/camera-orthographicsize.html
http://forum.unity3d.com/threads/screen-width-vs-camera-pixelwidth.136703/
c# unity3d 2d
No comments:
Post a Comment