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

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.kler.cn/a/453101.html

相关文章:

  • Java 中 getClass() 方法的使用与原理分析:深入理解对象类型信息
  • Hbase的特点、特性
  • 【求职面试】驾照的种类
  • Yolo11改策略:卷积改进|SAC,提升模型对小目标和遮挡目标的检测性能|即插即用
  • 宠物行业的出路:在爱与陪伴中寻找增长新机遇
  • 基于ceres优化的3d激光雷达开源算法
  • 使用Excel制作通达信自定义“序列数据“
  • cesium入门学习一
  • 江苏计算机专转本 技能Mysql知识点总结(一)
  • BiTCN-BiGRU基于双向时间卷积网络结合双向门控循环单元的数据多特征分类预测(多输入单输出)
  • 【Stable Diffusion】SD Stable Diffusion 最新版本 4.10 安装包
  • K8s 不同层次的进程间通信实现
  • Linux高级--2.1.2 select poll epoll reactor
  • 中科岩创边坡自动化监测解决方案
  • 34.正则表达式
  • 打包springBoot程序为exe(案例教程)
  • 每天40分玩转Django:实操在线商城
  • Spring Task的使用
  • 小程序canvas画环形百分比进度图
  • uni-app:监听页面返回,禁用返回操作
  • 【数据库初阶】数据库基础知识
  • 无人零售及开源 AI 智能名片 S2B2C 商城小程序的深度剖析
  • 怎么学习数据结构与算法?
  • 【前端实现pdf导出】
  • GESP202309 二级【小杨的 X 字矩阵】题解(AC)
  • 【大语言模型】ACL2024论文-35 WAV2GLOSS:从语音生成插值注解文本