【发布时间】:2018-10-24 02:38:32
【问题描述】:
我无法对齐我的玩家控制的摄像机(我对 FPS 和 TPS 使用相同的类)以查看目标(当另一个脚本调用 ResetCamera() 时)然后让玩家恢复控制。我这样做的主要原因是我可以在 FPS 和 TPS 相机之间切换并继续注视同一个目标。
我可以很好地查看目标,但只有在我设置lookAtTarget 之后,我停止在LateUpdate() 中根据yaw 和pitch(来自"Mouse X" 和"Mouse Y" 输入)设置旋转在ResetCamera(),但这意味着玩家不能再环顾四周。
但是,我无法弄清楚如何在此之后获得正确的 yaw 和 pitch 值,以便玩家可以继续从新的目标外观环顾四周。我该怎么做才能让玩家继续环顾四周?
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