2016-05-13 58 views
-1

我正在開發我的第一款遊戲,所以對Unity來說有點新鮮。所以我的問題是試圖通過按下按鈕來旋轉Fps控制器來面對目標。我設法通過安裝此代碼獨自旋轉的攝像頭,但僅在相機旋轉,因此徹底攪亂走我如何讓FPS控制器旋轉並面向目標

public class PlayerRotate : MonoBehaviour { 

    public Transform target; 

    void Update(){ 
     if(Input.GetKeyDown("r")){ 
      print ("Rotate"); 
      transform.LookAt(target.position); 
     } 
    } 
} 

我也試過將相同腳本的FPS控制器,但沒有任何反應。任何人的幫助將不勝感激,謝謝。

+0

Google FPS Controller Script ... http://answers.unity3d.com/questions/700918/how-to-get-my-camera-to-follow-first-person-pov-in。 html –

回答

0

FPSCaracterController旋轉由包含其資產的MouseLook.cs腳本驅動。 如果你打開它,你會看到,控制旋轉的功能:

public void LookRotation(Transform character, Transform camera) 

此方法會修改旋轉值直接用鼠標沒有任何中間操作的價值。所以,如果你想改變FPController的旋轉,你需要修改這個方法(或這個類)

例如,我做了這個修改使得FPController每次旋轉180度時用戶按下「U」鍵:

public void LookRotation(Transform character, Transform camera) 
{ 
    float yRot = CrossPlatformInputManager.GetAxis("Mouse X") * XSensitivity; 
    float xRot = CrossPlatformInputManager.GetAxis("Mouse Y") * YSensitivity; 

     m_CharacterTargetRot *= Quaternion.Euler(0f, yRot, 0f); 
     m_CameraTargetRot *= Quaternion.Euler(-xRot, 0f, 0f); 

     if (ORPargaModification) 
     { 
      if(Input.GetKeyDown(KeyCode.U)) 
      { 
       Debug.Log("U pressed"); 
       m_CharacterTargetRot *= Quaternion.Euler(0f, 180, 0f); 
      } 
      character.localRotation = m_CharacterTargetRot; 
      camera.localRotation = m_CameraTargetRot; 

     } 
     else 
     { 
      if (clampVerticalRotation) 
      m_CameraTargetRot = ClampRotationAroundXAxis(m_CameraTargetRot); 

      if (smooth) 
      { 
       character.localRotation = Quaternion.Slerp(character.localRotation, m_CharacterTargetRot, 
        smoothTime * Time.deltaTime); 
       camera.localRotation = Quaternion.Slerp(camera.localRotation, m_CameraTargetRot, 
        smoothTime * Time.deltaTime); 
      } 
      else 
      { 
       character.localRotation = m_CharacterTargetRot; 
       camera.localRotation = m_CameraTargetRot; 
      } 

     } 

     UpdateCursorLock(); 
    } 
+0

非常感謝您的答覆者,我一定會試一試 –