我该如何使用鼠标围绕玩家旋转相机?

3

现在它正在太空中旋转,我可以围绕它旋转摄像机,但不能围绕玩家旋转。我希望摄像机只能围绕玩家旋转。

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进行旋转。

创建一个以玩家为中心的对象,然后将摄像机作为该对象的子级添加进去。然后旋转围绕玩家的摄像机就像更改父对象的本地旋转一样简单。 - Abion47
@Abion47 我试过了:和我的脚本一样,但将这一行从:mainCameraTransform.eulerAngles = new Vector3(pitch, yaw, 0.0f); 改为 transform.parent.gameObject.transform.eulerAngles = new Vector3(pitch, yaw, 0.0f); 但它仍然旋转相机而不是我放在thirdpersoncontroller中心的空游戏物体。该脚本附加到作为GameObject子项的主相机上。 - Daniel Halfoni
将其附加到对象本身。采用这种方法,鼠标实际上控制的是对象,而相机只是随之移动。 - Abion47
我找到了如何做到这一点。 - Daniel Halfoni
1个回答

1

如评论中所述,您需要创建一个相机的父对象,并旋转该对象而不是相机本身。尝试使用以下代码:

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);
    }
}

找到了如何做。通过在玩家中心添加一个立方体来实现。效果很好。我还添加了缩放功能。 - Daniel Halfoni
@Abion47 我已经编辑了代码,使得相机的父对象在Start方法中被实例化,并且删除了改变相机位置的那行代码,现在它完美地工作了。我不理解你说的对象需要在项目视图中创建的说法。你是否熟悉在代码中创建游戏对象的概念? - lockstock

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接