unity中的交互控制脚本

使用方法: 挂载到要控制的相机上

target 选定围绕中心物体

csharp 复制代码
using UnityEngine;

public class OrbitCameraController : MonoBehaviour
{
    public Transform target; // 目标物体的Transform
    public float distance = 10.0f; // 初始相机到目标的距离

    public float xSpeed = 250.0f;
    public float ySpeed = 100.0f;

    [Range(1.0f, 100.0f)]
    public float minDistance = 2.0f; // 最小距离
    [Range(1.0f, 100.0f)]
    public float maxDistance = 40.0f; // 最大距离

    [Range(-90, 90)]                    // 控制俯仰角范围 [-90, 90]
    public float minYAngle = -60f;      // 最小俯仰角(向下看)
    [Range(-90, 90)]
    public float maxYAngle = 60f;       // 最大俯仰角(向上看)

    // 阻尼设置
    public bool useDamping = true;
    public float dampingTime = 0.15f;

    private float x = 0.0f;
    private float y = 0.0f;

    private float targetX = 0.0f;
    private float targetY = 0.0f;
    private float targetDistance;

    private Vector3 velocity = Vector3.zero;

    void Start()
    {
        Vector3 angles = transform.eulerAngles;
        x = angles.y;
        y = ClampAngle(angles.x, -360, 360);

        targetX = x;
        targetY = y;
        targetDistance = distance;

        //Cursor.lockState = CursorLockMode.Locked;
    }

    void LateUpdate()
    {
        if (!target) return;

        // 处理鼠标拖动视角
        HandleMouseRotation();

        // 处理鼠标滚轮缩放
        HandleScrollWheelZoom();

        // 处理触控缩放
        HandleTouchInput();

        // 应用阻尼效果
        if (useDamping)
        {
            x = Mathf.SmoothDamp(x, targetX, ref velocity.x, dampingTime);
            y = Mathf.SmoothDamp(y, targetY, ref velocity.y, dampingTime);
            distance = Mathf.SmoothDamp(distance, targetDistance, ref velocity.z, dampingTime);
        }
        else
        {
            x = targetX;
            y = targetY;
            distance = targetDistance;
        }

        // 计算相机位置与旋转
        Quaternion rotation = Quaternion.Euler(y, x, 0);
        Vector3 position = rotation * new Vector3(0, 0, -distance) + target.position;

        transform.rotation = rotation;
        transform.position = position;
    }

    void HandleMouseRotation()
    {
        if (Input.GetMouseButton(0)) // 鼠标左键拖动旋转
        {
            targetX += Input.GetAxis("Mouse X") * xSpeed * Time.deltaTime * 0.5f;
            targetY -= Input.GetAxis("Mouse Y") * ySpeed * Time.deltaTime * 0.5f;

            targetY = Mathf.Clamp(targetY, minYAngle, maxYAngle); // 限制上下角度
        }
    }

    void HandleScrollWheelZoom()
    {
        float scroll = Input.GetAxis("Mouse ScrollWheel");
        if (scroll != 0)
        {
            targetDistance -= scroll * 2f; // 滚轮灵敏度
            targetDistance = Mathf.Clamp(targetDistance, minDistance, maxDistance);

            UnityEngine.Debug.Log("相机距离: " + targetDistance);
        }
    }

    void HandleTouchInput()
    {
        if (Input.touchCount == 2)
        {
            Touch touchZero = Input.GetTouch(0);
            Touch touchOne = Input.GetTouch(1);

            Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
            Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;

            float prevMagnitude = (touchZeroPrevPos - touchOnePrevPos).magnitude;
            float currentMagnitude = (touchZero.position - touchOne.position).magnitude;

            float difference = currentMagnitude - prevMagnitude;
            float scaleFactor = difference * 0.01f;

            targetDistance -= scaleFactor;
            targetDistance = Mathf.Clamp(targetDistance, minDistance, maxDistance);
        }
    }

    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);
    }
}
相关推荐
龚子亦6 小时前
【Unity开发】热更新学习——AssetBundle
学习·unity·游戏引擎
萘柰奈8 小时前
Unity学习----【数据持久化】二进制数据(五)--由Excel自动生成数据结构类与二进制文件
数据结构·学习·unity
LHX sir8 小时前
低代码革命遇瓶颈?这个“套娃神技“才是破局关键!
前端·ui·前端框架·交互·团队开发·软件需求·极限编程
心前阳光8 小时前
Unity通过Object学习原型模式
学习·unity·原型模式
尤齐9 小时前
相机在两个机械臂上安装方式比较
深度学习·数码相机·机器人
列兵阿甘9 小时前
知微传感Dkam系列3D相机SDK例程篇:CSharp连接相机及保存数据
数码相机·3d
LHX sir10 小时前
巨头撤退,玩家内卷!2025,IoT平台的生死劫与重生路
开发语言·前端·物联网·低代码·ui·前端框架·交互
SmalBox12 小时前
【URP】[投影Projector]解析与应用
unity·渲染
云卓SKYDROID14 小时前
无人机传感器技术要点与难点解析
人工智能·数码相机·无人机·高科技·云卓科技·固件升级