Unity 3d跟随相机

时间:2014-10-02 17:49:21

标签: c# unity3d monodevelop

让这个相机在我的目标物体上运作完美,但需要注意一点。似乎无法让角色向上和向下看。左右移动完美,上下都不动。我在"Mouse Y"部分做错了什么?

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

2 个答案:

答案 0 :(得分:1)

您在verticle电话中没有使用Transform.Rotate(垂直?)。 修改抱歉,我刚刚遇到第一个问题时就停止了查看,进一步研究了另一个与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
}

要提供更多信息,您必须使用此代码说明最终目标的内容,因为仔细观察我会发现"奇怪&#34 ;代码示例正在尝试做什么。

答案 1 :(得分:0)

最后一行代码(transform.Lookat)覆盖了以前的代码......基本上说“无论发生什么事情都会看到目标”。