2014-10-02 104 views
1

讓我的目標物體具有完美的攝像頭,只需一個警告即可。似乎無法讓角色上下查看。左右移動完美,上下移動根本不動。我在做什麼錯了"Mouse Y"部分?Unity3D跟隨攝像頭

public GameObject target; 
    public float rotateSpeed = 7; 
    Vector3 offset; 

    void Start() { 
     offset = target.transform.position - transform.position; 
    } 

    void LateUpdate() { 
     float horizontal = Input.GetAxis("Mouse X") * rotateSpeed * Time.deltaTime; 
     float verticle = Input.GetAxis("Mouse Y") * rotateSpeed * Time.deltaTime; 
     target.transform.Rotate(0, horizontal, 0); 

     float desiredAngle = target.transform.eulerAngles.y; 

     Quaternion rotation = Quaternion.Euler(0, desiredAngle, verticle); 
     transform.position = target.transform.position - (rotation * offset); 

     transform.LookAt(target.transform); 
    } 

回答

1

你不是在你的Transform.Rotate通話使用verticle(垂直?)。 編輯:對不起,我剛剛停止尋找,一旦我遇到第一個問題,進一步看有另一個問題,類似於我在this question中解決的問題。在「操作順序」是錯誤的(見新評論):

public GameObject target; 
public float rotateSpeed = 7; 
Vector3 offset; 

void Start() { 
    offset = target.transform.position - transform.position; 
} 

void LateUpdate() { 
    float horizontal = Input.GetAxis("Mouse X") * rotateSpeed * Time.deltaTime; 
    float verticle = Input.GetAxis("Mouse Y") * rotateSpeed * Time.deltaTime; 
    //You didn't use verticle before. Depending on your model/setup you might verticle as the 3rd (Z) parameter to get rotation in the right direction 
    target.transform.Rotate(verticle, horizontal, 0); //This line rotates the transform 

    float desiredAngle = target.transform.eulerAngles.y; 

    Quaternion rotation = Quaternion.Euler(0, desiredAngle, verticle); 
    transform.position = target.transform.position - (rotation * offset); 

    transform.LookAt(target.transform); //This sets the absolute rotation of the transform. This call will "override" the above call to Rotate 
} 

爲了提供更多的信息,你得給一個什麼樣的最終目標是這段代碼的解釋,因爲仔細一看我看到代碼示例試圖做什麼的「古怪」。

+0

之前嘗試過。似乎想要移動,但沒有。屏幕變得像是試圖向上或向下移動,但隨後重新調整播放器。 – Volearix 2014-10-02 20:50:58

+0

實際上,這個特殊的代碼只是讓我的玩家在x軸上旋轉,而相機卻不遵循......:\ – Volearix 2014-10-02 20:53:03

+0

我添加了一些註釋來解釋它爲什麼不移動,但是您需要添加一些信息這個問題。 – 2014-10-03 01:32:06

0

最後一行代碼(transform.Lookat)覆蓋了以前的代碼......基本上說'無論發生什麼事情總是看目標'。