【问题标题】:Unity camera problem - Moving FPS/TPS camera to look at a target and resuming player controlUnity 相机问题 - 移动 FPS/TPS 相机以查看目标并恢复玩家控制
【发布时间】:2018-10-24 02:38:32
【问题描述】:

我无法对齐我的玩家控制的摄像机(我对 FPS 和 TPS 使用相同的类)以查看目标(当另一个脚本调用 ResetCamera() 时)然后让玩家恢复控制。我这样做的主要原因是我可以在 FPS 和 TPS 相机之间切换并继续注视同一个目标。

我可以很好地查看目标,但只有在我设置lookAtTarget 之后,我停止在LateUpdate() 中根据yawpitch(来自"Mouse X""Mouse Y" 输入)设置旋转在ResetCamera(),但这意味着玩家不能再环顾四周。 但是,我无法弄清楚如何在此之后获得正确的 yawpitch 值,以便玩家可以继续从新的目标外观环顾四周。我该怎么做才能让玩家继续环顾四周?

public class PlayerCamera : MonoBehaviour {

    public float mouseSensitivity = 10f;
    public Transform target;
    public float dstFromTarget = 2f;
    public Vector2 pitchConstraints = new Vector2(-20f, 85f);

    public float rotSmoothTime = .12f;
    Vector3 rotSmoothVel;
    Vector3 currRot;

    float yaw;
    float pitch;

    void LateUpdate() {
        yaw += Input.GetAxis("Mouse X") * mouseSensitivity;
        pitch -= Input.GetAxis("Mouse Y") * mouseSensitivity;
        pitch = Mathf.Clamp(pitch, pitchConstraints.x, pitchConstraints.y);

        currRot = Vector3.SmoothDamp(currRot, new Vector3(pitch, yaw), ref rotSmoothVel, rotSmoothTime);
        transform.eulerAngles = currRot;

        transform.position = target.position - transform.forward * dstFromTarget;
    }

    public void ResetCamera(Transform lookAtTarget) {
        transform.LookAt(lookAtTarget);

        // below gets yaw and pitch values that move the camera to look at the
        // wrong location
        // yaw = transform.eulerAngles.x;
        // pitch = transform.eulerAngles.y;
        // pitch = Mathf.Clamp(pitch, pitchConstraints.x, pitchConstraints.y);
        // currRot = new Vector3(pitch, yaw);
    }
}

【问题讨论】:

    标签: unity3d


    【解决方案1】:

    所以,我想出了如何做我想做的事。下面是第三人称或第一人称的脚本(只需将 dstFromTarget 变量调整为零)相机可以通过另一个脚本使用 ResetCamera() 旋转以查看目标,并且玩家可以从该点继续移动。

    public class PlayerCamera : MonoBehaviour {
    
    public bool isFirstPerson = false;
    public float mouseSensitivity = 10f;
    public Transform target;
    public float dstFromTarget = 2f;
    public bool invertedPitch = false;
    public float smoothTime = .12f;
    
    public float minimumX = -85f;
    public float maximumX = 85f;
    
    // LateUpdate, so the Camera Target will definitely have been set
    void LateUpdate () {
        // first person updating is handled by Inverse kinematics stuff in my case
        if (isFirstPerson) return;
        UpdateStuff();
    }
    
    public void UpdateStuff() {
        float yaw = Input.GetAxisRaw("Mouse X");
        float pitch = -Input.GetAxisRaw("Mouse Y");
        if (invertedPitch) {
            pitch *= -1f;
        }
    
        Vector3 yRot = new Vector3(0f, yaw, 0f) * mouseSensitivity;
        Vector3 xRot = new Vector3(pitch, 0f, 0f) * mouseSensitivity;
    
        xRot = ClampRotationAroundXAxis(transform.rotation, xRot);
    
        Transform newTrans = transform;
        newTrans.Rotate(xRot);
        newTrans.Rotate(yRot, Space.World);
    
        transform.rotation = Quaternion.Slerp(transform.rotation, newTrans.rotation, smoothTime * Time.deltaTime);
    
        transform.position = target.position - transform.forward * dstFromTarget;
    }
    
    public void ResetCamera(Transform lookAtTarget) {
        transform.LookAt(lookAtTarget);
    }
    
    Vector3 ClampRotationAroundXAxis(Quaternion q, Vector3 xrot) {
        q.x /= q.w;
        q.y /= q.w;
        q.z /= q.w;
        q.w = 1.0f;
    
        float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan (q.x);
    
        if (angleX < minimumX && xrot.x < 0f) {
            xrot = Vector3.zero;
        }
        if (angleX > maximumX && xrot.x > 0f) {
            xrot = Vector3.zero;
        }
    
        return xrot;
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-22
      • 2012-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多