【问题标题】:Unity 3d Follow cameraUnity 3d 跟随相机
【发布时间】:2014-10-02 17:49:20
【问题描述】:

请注意,让这台相机在我的目标对象上完美运行。似乎无法让角色向上和向下看。左右移动完美,上下不动。我在 "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);
    }

【问题讨论】:

    标签: c# unity3d monodevelop


    【解决方案1】:

    您没有在Transform.Rotate 调用中使用verticle(垂直?)。 编辑:抱歉,我刚遇到第一个问题就停止寻找,进一步看还有另一个问题,与我在this question 中解决的问题类似。 “操作顺序”是错误的(参见新的 cmets):

    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
    }
    

    要提供更多信息,您必须解释您的最终目标是什么,因为仔细观察我会发现代码示例试图实现的“奇怪”做。

    【讨论】:

    • 以前试过这个。好像想动但又不想动。屏幕开始抖动,就像它试图向上或向下移动一样,但随后又重新聚焦在玩家身上。
    • 实际上这个特定的代码只是让我的播放器在 x 轴上旋转并且相机不跟随......:\
    • 我添加了一些 cmets 来解释它为什么不动,但您需要在问题中添加一些信息。
    【解决方案2】:

    最后一行代码 (transform.Lookat) 覆盖了前面的代码...基本上是说“无论发生什么,都始终查看目标”。

    【讨论】:

      猜你喜欢
      • 2021-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 2021-04-21
      • 1970-01-01
      相关资源
      最近更新 更多