首页 > 其他分享 >unity摄像机围绕屏幕中心旋转,平移,缩放

unity摄像机围绕屏幕中心旋转,平移,缩放

时间:2023-03-20 15:55:49浏览次数:41  
标签:平移 缩放 float transform unity dx dy public euler

很好用所以保存下来使用

直接附代码了

3D摄像机的

using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Camera3DMove : MonoBehaviour
{
    //摄像机绕屏幕中心旋转缩放平移脚本
    public float thetaSpeed = 150.0f; //x旋转速度
    public float phiSpeed = 150.0f; //垂直旋转速度
    public float moveSpeed = 10.0f; //平移速度
    public float zoomSpeed = 10.0f; //缩放速度

    public float phiBoundMin = -89f;  //最低垂直旋转角度
    public float phiBoundMax = 89f; //最高垂直旋转角度

    //缩放距离限制 最小,最大
    public Vector2 distanceLimit = new Vector2(2f, 30f);

    //旋转平滑
    public float rotateSmoothing = 0.5f;
    //移动平滑
    public float moveSmoothing = 0.7f;

    //当前距离
    public float distance = 15.0f;

    //摄像机的初始角度
    private Vector2 euler;

    //目标旋转角度
    public Quaternion targetRot;
    //当前朝向的目标
    private Vector3 targetLookAt;
    //当前摄像机到目标的距离
    public float targetDist;

    //临时变量
    private Vector3 distanceVec = new Vector3(0, 0, 0);

    //目标对象的变换
    public Transform target;

    //中心点
    public Vector3 pivotPoint = new Vector3(0, 0, 0);

  

    //限制鼠标范围 避免点击抖动
    private Vector2 limitMouseX = new Vector2(-5, 5);
    private Vector2 limitMouseY = new Vector2(-5, 5);

    //目标位置
    public Vector3 targetPos;

    public void Start()
    {
        //获取当前摄像机的角度
        Vector3 angles = transform.eulerAngles;
        euler.x = angles.y;
        euler.y = angles.x;
        //unity only returns positive euler angles but we need them in -90 to 90
        euler.y = Mathf.Repeat(euler.y + 180f, 360f) - 180f;

        //创建目标物体
        GameObject go = new GameObject("_CameraTarget");
        //设置隐藏并不保存
        go.hideFlags = HideFlags.HideAndDontSave | HideFlags.HideInInspector;

        //设置初始参数
        target = go.transform; //设置目标

        target.position = pivotPoint; //设置中心点为其实目标位置
        targetDist = (transform.position - target.position).magnitude; //获取相机到中心点的距离

        targetRot = transform.rotation; //设置目标旋转角度
        targetLookAt = target.position; //设置朝向的目标所在位置

    
    }
    public void Update()
    {                        
            //获取鼠标在屏幕的x.y值
            float dx = Input.GetAxis("Mouse X");
            float dy = Input.GetAxis("Mouse Y");

            //鼠标偏移量限制,防止抖动
            if (dx < limitMouseX.x || dx > limitMouseX.y)
            {              
                return;
            }

            if (dy < limitMouseY.x || dy > limitMouseY.y)
            {               
                return;
            }

            if (Input.GetMouseButton(2))
            {
                //计算移动量
                dx = dx * moveSpeed * 0.005f * targetDist;
                dy = dy * moveSpeed * 0.005f * targetDist;
                targetLookAt -= transform.up * dy + transform.right * dx;            
            }

            else if (Input.GetMouseButton(1))
            {
                //计算旋转角度
                dx = dx * thetaSpeed * 0.02f;
                dy = dy * phiSpeed * 0.02f;
                euler.x += dx;
                euler.y -= dy;
                //限制垂直旋转角度
                euler.y = ClampAngle(euler.y, phiBoundMin, phiBoundMax);
                targetRot = Quaternion.Euler(euler.y, euler.x, 0);
            }

            //当前摄像机到目标的距离 减鼠标滑动距离
            targetDist -= Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
            //限制缩放
            targetDist = Mathf.Clamp(targetDist, distanceLimit.x, distanceLimit.y);

        
    }

    public void FixedUpdate()
    {   
        //计算距离
        distance = moveSmoothing * targetDist + (1 - moveSmoothing) * distance;
        //计算旋转角度插值
        transform.rotation = Quaternion.Slerp(transform.rotation, targetRot, rotateSmoothing);

        //计算距离插值
        target.position = Vector3.Lerp(target.position, targetLookAt, moveSmoothing);

        //设置距离 Z轴
        distanceVec.z = distance;
        //设置相机最终的位置  目标位置 - 相机当前旋转角度 * 距离Z轴
        transform.position = target.position - transform.rotation * distanceVec;
    }

    /// <summary>
    /// 限制角度
    /// </summary>
    /// <param name="angle"></param>
    /// <param name="min"></param>
    /// <param name="max"></param>
    /// <returns></returns>
    static float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360f) angle += 360f;
        if (angle > 360f) angle -= 360f;
        return Mathf.Clamp(angle, min, max);
    }

}
摄像机脚本

在加一个2D摄像机的项目需要就一起记录下喽,不喜勿怪哦,

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

public class Camera2DMove : MonoBehaviour
{
    private Camera camera2D;

    public float rotation_H_speed =10 ;
    public float rotation_V_speed =10 ;
    // Start is called before the first frame update
    void Start()
    {
        camera2D = this.GetComponent<Camera>();

    }
    // Update is called once per frame
    void Update()
    {
        if (camera2D.enabled)
        {
            if (Input.GetMouseButton (2))
            {
                if (Input.GetMouseButton(2))
                {
                    this.transform.Translate(new Vector3(-Input.GetAxis("Mouse X") * rotation_H_speed*Time.deltaTime, -Input.GetAxis("Mouse Y") * rotation_V_speed*Time.deltaTime, 0f));
                }
            }
        }
    }
}
2D摄像机移动

 

有需要可以用哦,喜欢点个赞呗

标签:平移,缩放,float,transform,unity,dx,dy,public,euler
From: https://www.cnblogs.com/qq2351194611/p/17236589.html

相关文章