2016-07-23 84 views
1

目前,我有以下兩個問題:復位位置相對於旋轉

  • 的第一個問題是,當我恢復我的相機位置我也必須重新設置我的相機旋轉。這是因爲我的相機 偏移量設置爲與z軸和y軸 軸上的位置稍微偏離我的播放器,顯然這些值應根據我的相機旋轉 旋轉,儘管我不確定如何找出那些東西值應該是 。

  • 我的第二個問題是,我的旋轉使用光線投射找到 中間的屏幕,並確定其旋轉的起源雖然這似乎是 稍微偏離屏幕的中間,當它旋轉旋轉 原點的移動,如以及,如果它確實在屏幕中間 不應該完全靜止?也有更好,更少的實現我想要的旋轉的昂貴方式嗎?

相關的代碼:

void RotateCamera() 
{ 
    //Find midle of screen 
    Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width/2, Screen.height/2, 0)); 
    RaycastHit hitInfo; 

    //Checks if ray hit something 
    if (Physics.Raycast(ray, out hitInfo)) 
    { 
     //Rotate left and right 
     if (Input.GetKey(KeyCode.RightArrow)) 
     { 
      transform.RotateAround(hitInfo.point, -Vector3.up, rotationSpeed * Time.deltaTime); 
     } 
     if (Input.GetKey(KeyCode.LeftArrow)) 
     { 
      transform.RotateAround(hitInfo.point, Vector3.up, rotationSpeed * Time.deltaTime); 
     } 
    } 

    //Draws Raycast 
    Debug.DrawRay(ray.origin, ray.direction * 100, Color.yellow); 
} 

void ResetCameraPosition() 
{ 
    //Reset and lock camera position 
    transform.rotation = Quaternion.identity; 
    transform.position = player.transform.position + cameraOffset; 

} 

Image displaying what I mean

+0

你能解釋多一點你真的想讓相機做什麼?我想我會得到你想圍繞一個點旋轉,並根據需要重置當前面對的球員,但請確認。 – Absinthe

+0

是的,一個截圖或繪圖(甚至程序員藝術)將幫助很多 – 2016-07-23 19:36:11

+0

@Absinthe是的,很抱歉,基本上它是我想旋轉相機在屏幕的中間,所以基本上它圍繞着它旋轉看完全,我目前的代碼沒有問題,但它的焦點在旋轉時略微移動,它似乎是一種昂貴的計算方法。至於重置旋轉部分,我希望將我的相機重置爲已旋轉的方式,儘管它的定位方式可以看到我的角色。希望這可以清除它。謝謝! –

回答

0

使用Camera.ScreenToWorldPoint創建在其周圍樞轉屏幕的中間的 '目標'。刪除所有光線投射的東西,因爲你不需要它,並替換相關的位:

float rotationSpeed = 45; // or whatever speed 
float distance = 5f; // or whatever radius for the orbit 
Vector3 target = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width/2, Screen.height/2, distance)); 

if (Input.GetKey(KeyCode.RightArrow)) 
{ 
    transform.RotateAround(target , -Vector3.up, rotationSpeed * Time.deltaTime); 
} 
if (Input.GetKey(KeyCode.LeftArrow)) 
{ 
    transform.RotateAround(target , Vector3.up, rotationSpeed * Time.deltaTime); 
} 
+0

問題與該代碼是我不知道我的距離,因此,raycast,也不回答我的問題的第一和主要部分。 –