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);
    }
}
相关推荐
NIKITAshao几秒前
Unity URP Volume组件详解(笔记)
unity·游戏引擎
lingxiao168883 小时前
WebApi详解+Unity注入--下篇:Unity注入
unity·c#·wpf
世洋Blog3 小时前
面经-CPU、内存、GPU的性能优化
unity·性能优化
lingxiao168885 小时前
WebApi详解+Unity注入--中篇:.net core的WebAPI
unity·c#·.netcore
wenzhangli78 小时前
告别手撸架构图!AI+Ooder实现漂亮架构+动态交互+全栈可视化实战指南
人工智能·架构·交互
weixin_423995009 小时前
unity 处理图片:截图,下载,保存
java·unity·游戏引擎
qq_5260991311 小时前
机器视觉网卡的全面选型指南
数码相机·计算机视觉·自动化
呆呆敲代码的小Y12 小时前
【Unity实战篇】| 游戏轮播图效果,多种实现思路及完整教程
游戏·unity·游戏引擎·实战·游戏开发·轮播图·u3d
科技与数码14 小时前
数字人公司世优科技以全栈技术解锁政务文旅展厅全场景智能交互
科技·交互·政务
儿歌八万首14 小时前
鸿蒙自定义相机开发:Camera Kit
数码相机·华为·harmonyos·harmonyos app