2014-06-21 25 views
0

我剛開始製作一個2D遊戲使用統一,我有一個決議&凸輪問題。 我有一個二維地形,我試圖修復左側的地形凸輪的左側。但是,當我將屏幕分辨率更改爲iphone 4時,左側是隱藏的。我想這個腳本的凸輪,但它不工作:在所有屏幕分辨率的2D遊戲固定相機的左側

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

回答

0

你想用Camera.pixelRect(見Camera.pixelWidthCamera.pixelHeight)。

您正在世界座標中找到位於屏幕中心的世界點,基於此更改相機的視口大小,然後將相機的位置設置回屏​​幕中心。如果要將視口的左邊緣固定爲x = 0(假定您的地形的左邊緣位於x = 0處),則應該找到視口的xMax,將其從屏幕座標轉換爲世界座標,並將相機的x位置設置爲。

void Awake() 
{ 
    // Set the camera's orthographic size. 
    // The camera's pixelWidth, pixelHeight, and pixelRect is relative to it. 
    Vector3 worldCenter = Camera.main.ScreenToWorldPoint (new Vector3 (Screen.width/2, Screen.height/2, -10)); 
    Camera.main.orthographicSize = worldCenter.y; 

    // Get the viewport's rectangle and calculate the camera's desired position. 
    // We want to use viewportRect.xMax since we want to move the camera to the right by half of the 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; 
} 

一些參考:

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/