如何用鼠标在播放器周围旋转相机?

时间:2017-05-11 20:49:30

标签: 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 = 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进行轮换。

1 个答案:

答案 0 :(得分: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);
    }
}