C# 如何用鼠标围绕播放器旋转相机?
现在它在空间中旋转,我可以旋转相机,但不能旋转播放器。我希望相机只围绕播放器旋转C# 如何用鼠标围绕播放器旋转相机?,c#,unity3d,unity5,C#,Unity3d,Unity5,现在它在空间中旋转,我可以旋转相机,但不能旋转播放器。我希望相机只围绕播放器旋转 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 =
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);
}
}
创建一个以播放器为中心的对象,然后将相机作为子对象添加到该对象中。然后围绕播放机旋转摄影机就像更改父对象的局部旋转一样简单。@Abion47我尝试了:与脚本中的相同,但将行更改为:mainCameratTransform.eulerAngles=new Vector3(俯仰、偏航、0.0f);To transform.parent.gameObject.transform.eulerAngles=新矢量3(俯仰、偏航、0.0f);但它仍然在旋转摄像机,而不是我放在第三个人控制器中心的空游戏对象。脚本连接到游戏对象的子对象主摄像机。将其连接到对象本身。通过这种方法,对象就是鼠标实际控制的对象,而相机只是在骑行中移动。我发现了如何做到这一点。发现了如何做到这一点。在玩家的中心添加一个立方体。工作得很好。我还添加了“缩小”。@Abion47我编辑了代码,以便在Start方法中实例化cameraParent,并删除了更改相机位置的代码行,现在它工作得很好。我不理解您关于需要在项目视图中创建对象的说法。您熟悉在代码中创建游戏对象的想法吗?