2014-10-02 82 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)覆盖了以前的代码......基本上说'无论发生什么事情总是看目标'。