现在它在空间中旋转,我可以围绕但不能围绕玩家旋转相机。我希望相机只围绕玩家旋转。
using UnityEngine;
using System.Collections;
public class CameraMover : MonoBehaviour
{
public float speedH = 2.0f;
public float speedV = 2.0f;
private float yaw = 0.0f;
private float pitch = 0.0f;
public Transform playerTransform;
public Transform mainCameraTransform = null;
private Vector3 cameraOffset = Vector3.zero;
public float turnSpeed = 3;
void Start()
{
mainCameraTransform = Camera.main.transform;
//Get camera-player Transform Offset that will be used to move the camera
cameraOffset = mainCameraTransform.position - playerTransform.position;
}
void LateUpdate()
{
//Move the camera to the position of the playerTransform with the offset that was saved in the begining
mainCameraTransform.position = playerTransform.position + cameraOffset;
yaw += speedH * Input.GetAxis("Mouse X");
pitch -= speedV * Input.GetAxis("Mouse Y");
mainCameraTransform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
}
}
我现在使用 eulerAngles 进行旋转。
最佳答案
如评论中所述,您需要为相机创建一个父对象,并旋转该对象而不是相机。试试这个代码:
using UnityEngine;
using System.Collections;
public class CameraMover : MonoBehaviour
{
public float speedH = 2.0f;
public float speedV = 2.0f;
private float yaw = 0.0f;
private float pitch = 0.0f;
public Transform playerTransform;
public Transform mainCameraTransform = null;
private Vector3 cameraOffset = Vector3.zero;
public float turnSpeed = 3;
// Create a camera parent object
GameObject cameraParent;
void Start()
{
cameraParent = new GameObject();
mainCameraTransform = Camera.main.transform;
//Get camera-player Transform Offset that will be used to move the camera
cameraOffset = mainCameraTransform.position - playerTransform.position;
// Position the camera parent to the player and add the camera as a child
cameraParent.transform.position = playerTransform.position;
mainCameraTransform.parent = cameraParent.transform;
}
void LateUpdate()
{
//Move the camera to the position of the playerTransform with the offset that was saved in the begining
//mainCameraTransform.position = playerTransform.position + cameraOffset;
yaw += speedH * Input.GetAxis("Mouse X");
pitch -= speedV * Input.GetAxis("Mouse Y");
// Rotate the camera parent instead of the camera
cameraParent.transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
}
}
关于c# - 我怎样才能用鼠标围绕播放器旋转相机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43925283/