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

Unity模型观察脚本

移动相机脚本,围绕物体实现缩放和旋转

加了SmoothDamp实现缓动

public class ViewAndOperate : MonoBehaviour
{public GameObject target;public float rotateSpeed;public float zoomSpeed;public float rotateSmoothTime; // 缓冲时间参考值public float zoomSmoothTime; // 缩放平滑时间private float currentDistance;private float targetDistance;private float zoomVelocity;public float zoomMinDistance;public float zoomMaxDistance;public float zoomSize;//鼠标滚轮缩放尺度private float currentAngle;private float targetAngle;private float angleVelocity;public float pitchMin;public float pitchMax;private float targetPitch;private float currentPitch;private float pitchVelocity;private void Start(){currentDistance = Vector3.Distance(transform.position, target.transform.position);targetDistance = currentDistance;}private void Update(){float scrollInput = Input.GetAxis("Mouse ScrollWheel")* zoomSize;if (Input.GetMouseButton(1)){float horizontalInput = Input.GetAxis("Mouse X") * rotateSpeed * Time.deltaTime * 100;float verticalInput = -Input.GetAxis("Mouse Y") * rotateSpeed * Time.deltaTime * 100;targetAngle += horizontalInput;targetPitch += verticalInput;targetPitch = Mathf.Clamp(targetPitch, pitchMin, pitchMax);}float smoothAngle = Mathf.SmoothDamp(currentAngle, targetAngle, ref angleVelocity, rotateSmoothTime);float deltaAngle = smoothAngle - currentAngle;currentAngle = smoothAngle;float smoothPitch = Mathf.SmoothDamp(currentPitch, targetPitch, ref pitchVelocity, rotateSmoothTime);float deltaPitch = smoothPitch - currentPitch;currentPitch = smoothPitch;transform.RotateAround(target.transform.position, Vector3.up, deltaAngle);transform.RotateAround(target.transform.position, transform.right, deltaPitch);if (scrollInput != 0){targetDistance = Mathf.Clamp(targetDistance - scrollInput * zoomSpeed, zoomMinDistance, zoomMaxDistance);}currentDistance = Mathf.SmoothDamp(currentDistance, targetDistance, ref zoomVelocity, zoomSmoothTime);Vector3 zoomDirection = (transform.position - target.transform.position).normalized;transform.position = target.transform.position + zoomDirection * currentDistance;}
}


http://www.mrgr.cn/news/81333.html

相关文章:

  • 范冰冰担任第75届柏林电影节主竞赛单元评委 共鉴电影佳作
  • 进程通讯——类型和发展
  • HTML5+SVG+CSS3实现雪中点亮的圣诞树动画效果源码
  • 【时时三省】(C语言基础)对比一组函数
  • 16 分布式session和无状态的会话
  • Elasticsearch容器启动报错:AccessDeniedException[/usr/share/elasticsearch/data/nodes];
  • Android开发环境搭建和编译系统
  • 知识图谱嵌入大总结:难点、方法、工具、和图嵌入的区别
  • 【innodb 阅读笔记】之 数据页结构介绍
  • springboot容器无法获取@Autowired对象,报null对象空指针问题的解决方式
  • Element-plus表格使用总结
  • 5、mysql的读写分离
  • Docker数据库的主从复制
  • 基于springboot的海洋知识服务平台的设计与实现
  • HuaWei、NVIDIA 数据中心 AI 算力对比
  • ThinkPHP接入PayPal支付
  • Kibana:LINUX_X86_64 和 DEB_X86_64两种可选下载方式的区别
  • RT-DETR学习笔记(2)
  • CTFHub disable_functions通关
  • 华为路由器AR101W-S
  • go语言并发文件备份,自动比对自动重命名(逐行注释)
  • Require:离线部署 Sourcegraph
  • Linux驱动开发--字符设备驱动开发
  • STM32 高级 谈一下IPV4/默认网关/子网掩码/DNS服务器/MAC
  • c++类型判断和获取原始类型
  • Flutter 实现全局悬浮按钮学习