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);
    }
}
相关推荐
Robbie丨Yang4 小时前
【Unity 入门教程】二、核心概念
unity·游戏引擎
陈言必行10 小时前
Unity 性能优化 之 实战场景简化(LOD策略 | 遮挡剔除 | 光影剔除 | 渲染流程的精简与优化 | Terrain地形优化 | 主光源级联阴影优化)
unity·性能优化·游戏引擎
351868039913 小时前
鸿蒙音乐应用开发:音频播放与UI交互实战
华为·音视频·交互·harmonyos·arkts
张艾拉 Fun AI Everyday13 小时前
AI + 制造:AI 如何重构制造业的质检与排产流程
数码相机
爱吃小胖橘13 小时前
Unity-动画基础
unity·c#·游戏引擎
陈言必行14 小时前
Unity 性能优化 之 内存优化
unity·性能优化·游戏引擎
ellis197014 小时前
LuaC API开发环境搭建保姆级教程
c++·unity·lua
绀目澄清16 小时前
Unity 游戏引擎中 HDRP(高清渲染管线) 的材质着色器选择列表
unity·游戏引擎·材质
患得患失94917 小时前
【Threejs】【工具类】Raycaster实现 3D 交互(如鼠标拾取、碰撞检测)的核心工具
3d·交互·threejs·raycaster
ellis197018 小时前
toLua[二] Examples 01_HelloWorld分析
unity·c#·lua