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);
    }
}
相关推荐
weixin_424294672 天前
Unity 调用Steamworks API 的 SteamUserStats.RequestCurrentStats()报错
unity·游戏引擎·steamwork
HoFunGames2 天前
Unity小地图,Easy Minimap System MT-GPS插件
unity·游戏引擎
微学AI2 天前
从云端到指尖:重构 AI 终端生态与实体交互新范式
人工智能·重构·交互
The️2 天前
Linux驱动开发之Read_Write函数
linux·运维·服务器·驱动开发·ubuntu·交互
wy3258643642 天前
Unity 新输入系统InputSystem(基本操作)
unity·c#·游戏引擎
WarPigs2 天前
着色器multi_compile笔记
unity·着色器
ECHO飞跃 0122 天前
Unity2019 本地推理 通义千问0.5-1.5B微调导入
人工智能·深度学习·unity·llama
Unity游戏资源学习屋2 天前
【Unity UI资源包】GUI Pro - Casual Game 专为休闲手游打造的专业级UI资源包
ui·unity
工业相机定制与开发2 天前
短波红外相机KGSMT30GI在食品与光伏产业中的应用优势
数码相机
雪人不是菜鸡2 天前
MTF曲线图分析
数码相机