我无法对齐播放器控制的相机(我对FPS和TPS使用相同的类)以查看目标(当ResetCamera()
被另一个脚本调用时),然后让播放器恢复控制。我这样做的主要原因是,我可以在FPS和TPS摄像机之间切换并保持瞄准相同的目标。
我可以很好地查看目标,但是只有当我停止根据{{1}和yaw
和pitch
输入中的"Mouse X"
和"Mouse Y"
设置旋转度时, 1}},然后在LateUpdate()
中设置lookAtTarget
,但这意味着播放器无法再环顾四周。
但是,在此之后,我无法弄清楚如何获取正确的ResetCamera()
和yaw
值,因此玩家可以继续从新的目标视线中寻找目标。我该怎么做才能让玩家继续环顾四周?
pitch
答案 0 :(得分:0)
因此,我想出了如何做自己想做的事。下面是第三人称或第一人称(只需将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;
}
}