c# - 我怎样才能用鼠标围绕播放器旋转相机?

标签 c# unity3d

现在它在空间中旋转,我可以围绕但不能围绕玩家旋转相机。我希望相机只围绕玩家旋转。

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/

相关文章:

c# - 减少对 SignalR、NHibernate 和 Ninject 的依赖

C# Linq 合并两个字典

unity3d - 如何将 RectTransform(UI 面板)转换为屏幕坐标?

unity3d - 将内容缩放到最大的 UI 元素(Unity)

c# - 转换日期时间时区以进行显示的正确方法

c# - 自定义测试适配器未在测试资源管理器上显示测试

c# - 在 C# 中的方法中定义枚举?

c# - 如何修复 UNITYTLS_X509VERIFY_NOT_DONE

c# - 无法序列化我的游戏统计数据

regex - 这是正则表达式吗?