Unity三种摄像机旋转方式

1.按下鼠标右键可以实现摄像机上下左右旋转

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraRotate : MonoBehaviour
{
    //旋转速度
    public float rotationSpeed = 5f;
    //上下旋转角度限制
    public float maxVerticalAngle = 90f;
    public float minVerticalAngle = -90f;
    //旋转缓冲速度
    public float lerpSpeed = 10f;
    private float targetRotationX = 0f;
    private float targetRotationY = 0f;
    void Update()
    {
        if (Input.GetMouseButton(1))
        {
            // 获取鼠标输入的旋转增量
            float rotationXInput = -Input.GetAxis("Mouse Y");
            float rotationYInput = Input.GetAxis("Mouse X");
            // 根据旋转速度进行摄像机的旋转
            targetRotationX += rotationXInput * rotationSpeed;
            targetRotationY += rotationYInput * rotationSpeed;
            // 对上下旋转角度进行限制
            targetRotationX = Mathf.Clamp(targetRotationX, minVerticalAngle, maxVerticalAngle);
            // 根据旋转角度更新摄像机的欧拉角,Quaternion.Lerp可以使摄像机旋转更加平滑
            Quaternion targetRotation = Quaternion.Euler(targetRotationX, targetRotationY, 0f);
            transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, lerpSpeed * Time.deltaTime);
        }
    }
}

2.按下鼠标右键可以实现摄像机围绕某个物体上下左右旋转

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraRotate1 : MonoBehaviour
{
    public Transform target;
    public float rotationSpeed = 5f;
    public float maxVerticalAngle = 90f;
    public float minVerticalAngle = -90f;
    public float lerpSpeed = 200f;
    public float distance = 10;

    private float targetRotationX = 0f;
    private float targetRotationY = 0f;

    void Start()
    {
        if (target == null)
            Debug.LogError("Please assign a target to the orbit camera!");
    }
    void Update()
    {
        if (Input.GetMouseButton(1))
        {
            float rotationXInput = -Input.GetAxis("Mouse Y");
            float rotationYInput = Input.GetAxis("Mouse X");

            targetRotationX += rotationXInput * rotationSpeed;
            targetRotationY += rotationYInput * rotationSpeed;

            targetRotationX = Mathf.Clamp(targetRotationX, minVerticalAngle, maxVerticalAngle);

            Quaternion targetRotation = Quaternion.Euler(targetRotationX, targetRotationY, 0f);
            transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, lerpSpeed * Time.deltaTime);
        }
        transform.position = target.position - transform.forward * distance;
    }
}

3.摄像头始终跟随在某个物体的正后方

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraRotate2 : MonoBehaviour
{
    public Transform target;
    public float followSpeed = 2f;
    public float followHeight = 4f;
    public float distance = 8f;

    private Vector3 velocity = Vector3.zero;
    void Start()
    {
        if (target == null)
            Debug.LogError("Please assign a target to the orbit camera!");
    }
    void LateUpdate()
    {
        Vector3 targetPosition = target.position - (target.forward * distance)+new Vector3(0,followHeight,0);
        transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, 1f / followSpeed);
        transform.LookAt(target);
    }
}

猜你喜欢

转载自blog.csdn.net/falsedewuxin/article/details/131657900