当前位置: 首页 > news >正文

unity中的交互控制脚本

使用方法: 挂载到要控制的相机上
target 选定围绕中心物体

using UnityEngine;public class OrbitCameraController : MonoBehaviour
{public Transform target; // 目标物体的Transformpublic 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);}
}
http://www.dtcms.com/a/364385.html

相关文章:

  • 如何选择适合企业的海外智能客服系统:6 大核心维度 + 实战选型指南
  • 【STL源码剖析】从源码看 deque :拆解双端队列的底层实现与核心逻辑
  • 用友T3、T6/U8批量作废凭证
  • 从数据生成到不确定性估计:用 LSTM + 贝叶斯优化实现时间序列多步预测
  • 基于SpringBoot的旅游管理系统
  • 【大前端】React 使用 Redux 实现组件通信的 Demo 示例
  • React实现点击按钮复制操作【navigator.clipboard与document.execCommand】
  • 基于单片机PWM信号发生器系统Proteus仿真(含全部资料)
  • 平衡车 - 电机调速
  • 基于单片机车内换气温度检测空气质量检测系统Proteus仿真(含全部资料)
  • 单片机点灯
  • Linux 网络编程中核心函数`recv`。
  • zynq 开发系列 新手入门:GPIO 连接 MIO 控制 LED 闪烁(SDK 端代码编写详解)
  • Spring Boot 实现数据库表变更监听的 Redis 消息队列方案
  • 单片机控制两只直流电机正反转C语言
  • 变频器实习DAY42 VF与IF电机启动方式
  • Excel 电影名匹配图片路径教程:自动查找并写入系统全路径
  • wpf 自定义控件,只能输入小数点,并且能控制小数点位数
  • 机器学习从入门到精通 - Python环境搭建与Jupyter魔法:机器学习起航必备
  • 如何在modelscope上上传自己的MCP服务
  • 【收藏】2025 前端开发者必备 SVG 资源大全
  • 【2025ICCV-持续学习方向】一种用于提示持续学习(Prompt-based Continual Learning, PCL)的新方法
  • 【CouponHub开发记录】SpringAop和分布式锁进行自定义注解实现防止重复提交
  • RAG|| LangChain || LlamaIndex || RAGflow
  • kafka概念之间关系梳理
  • mac idea 配置了Gitlab的远程地址,但是每次pull 或者push 都要输入密码,怎么办
  • 项目中常用的git命令
  • python基础案例-数据可视化
  • Streamlit 数据看板模板:非前端选手快速搭建 Python 数据可视化交互看板的实用工具
  • 【Linux】为什么死循环卡不死 Linux?3 个核心逻辑看懂进程优先级与 CPU 调度密码