Unity开发 360度查看模型的两种方式

这篇文章介绍了在Unity3D中如何根据不同平台(Android、iOS或桌面)控制相机和模型的旋转。通过判断平台类型设置不同的旋转速度,使用触摸或鼠标输入来调整视角或模型角度。提供了Awake、Update方法以及重置功能,适用于有动画的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.旋转相机

    private float rotateSpeed; // 旋转速度
    private bool isPlatform;
    private Quaternion cameraInitRota;//相机初始角度
    private Vector3 cameraInitPos;//相机初始位置
    public Transform target;//人物父物体
    public Transform cameraTransform; // 相机对象
    public bool isRota;

  private void Awake()
    {
       
        cameraInitRota = cameraTransform.localRotation;
        cameraInitPos = cameraTransform.localPosition;
        JudgPlatform();

    }

    private void Update()
    {
        RotaRole();
    }
    /// <summary>
    /// 重置视角
    /// </summary>
    public void ResetFar()
    {
        cameraTransform.localRotation = cameraInitRota;
        cameraTransform.localPosition = cameraInitPos;
    }

/// <summary>
    /// 判断当前平台设置对应旋转速度
    /// </summary>
    private void JudgPlatform()
    {
        isRota = false;
        if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
        {
            isPlatform = true;
            rotateSpeed = 0.2f;

        }
        else
        {
            rotateSpeed = 5f;
        }
    }
    /// <summary>
    /// 旋转模型
    /// </summary>
    private void RotaRole()
    {
        if (!isRota) return;
        if (isPlatform)
        {
            // 处理触摸输入
            if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
            {
                // 在屏幕上滑动手指,旋转相机
                Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
                float rotationX = touchDeltaPosition.x * rotateSpeed;
                float rotationY = -touchDeltaPosition.y * rotateSpeed;
                cameraTransform.transform.RotateAround(target.transform.position, Vector3.up, rotationX);
                cameraTransform.transform.RotateAround(target.transform.position, cameraTransform.transform.right, rotationY);
            }
        }
        else
        {
            // 处理鼠标输入
            if (Input.GetMouseButton(1))
            {
                // 在屏幕上滑动鼠标,旋转相机
                float rotationX = Input.GetAxis("Mouse X") * rotateSpeed;
                float rotationY = -Input.GetAxis("Mouse Y") * rotateSpeed;
                cameraTransform.transform.RotateAround(target.transform.position, Vector3.up, rotationX);
                cameraTransform.transform.RotateAround(target.transform.position, cameraTransform.transform.right, rotationY);
            }
        }
    }

二.旋转模型


  
    private float rotateSpeed; // 旋转速度
    private bool isPlatform;//判断平台
    private Quaternion modelInitRota;//模型初始角度
    private Vector3 modelInitPos;//模型初始位置

    private float lastMouseX; // 上一帧鼠标的 X 坐标

    public bool isRota;//指定页面旋转值
    public Transform target;//人物父物体
  
   


    private void Awake()
    {
        rolePath = Application.persistentDataPath + "/File/";
        modelInitRota = target.localRotation;
        modelInitPos = target.localPosition;
        JudgPlatform();

    }

    private void Update()
    {
        RotaRole();
    }
    /// <summary>
    /// 重置视角
    /// </summary>
    public void ResetFar()
    {
        target.localRotation = modelInitRota;
        target.localPosition = modelInitPos;
    }
    /// <summary>
    /// 判断当前平台设置对应旋转速度
    /// </summary>
    private void JudgPlatform()
    {
        isRota = false;
        if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
        {
            isPlatform = true;
            rotateSpeed = 0.2f;

        }
        else
        {
            isPlatform = false;
            rotateSpeed = 1f;
        }
    }
    /// <summary>
    /// 旋转模型
    /// </summary>
    private void RotaRole()
    {
        if (!isRota) return;
        if (isPlatform)
        {
            if (Input.touchCount == 1) 
            {
                // 获取触摸的移动量
                Touch touch = Input.GetTouch(0);
                float deltaX = touch.deltaPosition.x;

                // 计算旋转角度
                float rotation = deltaX * rotateSpeed;

                // 应用旋转
                target.Rotate(0, rotation, 0);

             
            }
        }
        else
        {
            if (Input.GetMouseButton(1)) // 鼠标右键
            {
                // 获取鼠标的移动量
                float deltaX = Input.mousePosition.x - lastMouseX;
                lastMouseX = Input.mousePosition.x;

                // 计算旋转角度
                float rotation = deltaX * rotateSpeed;

                // 应用旋转
                target.Rotate(0, rotation, 0);
            }
        }
    }

两种方式按需求去用,摄像机旋转适合模型有动画的清空下使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

芊泽散人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值