通过检测鼠标是否在指定UI上拖动 实现围绕角色旋转摄像机(包括安卓端)

通过检测鼠标是否在指定UI上拖动 实现围绕角色旋转摄像机(包括安卓端)

在这里插入图片描述

  1. 使用的虚拟摄像机
  2. UI使用的单独的摄像机,玩家使用单独的相机
  3. 判断输入点是否在指定UI上时要注意把UI坐标转换为屏幕坐标,要指定摄像机
RectTransformUtility.RectangleContainsScreenPoint(rect_RotateCameraArea, Input.mousePosition, GameEntry.MyCamera.uiCamera);
  1. 虚拟摄像机进行如下设置,**需要注意的是**此功能相机并非跟随、锁定的Player,而是创建了个CameraRoot空物体,让CameraRoot跟随Player的位置,而旋转值不跟随。为什么不直接作为Player的子物体呢? 因为,这个项目人物通过摇杆移动,操作摇杆时人物也会旋转,CameraRoot也就跟着旋转了
    在这里插入图片描述
  2. 具体功能代码
        #region 旋转视角
        private float x, y;
        private bool isDragging;
        private Vector2 lastTouchPosition;
        private float touchSensitivity=0.2f;
        private float mouseSensitivity=0.2f;
        void RotateCamera_VirtualCamera()
        {
            
            bool isInAllowedArea = RectTransformUtility.RectangleContainsScreenPoint(rect_RotateCameraArea, Input.mousePosition, GameEntry.MyCamera.uiCamera);
            if (!isInAllowedArea)
            {
                return; // 不在限定区域时不执行相机旋转逻辑
            }
            
#if UNITY_EDITOR // 编辑器中的鼠标旋转逻辑
            if (Input.GetMouseButtonDown(0)) // 鼠标左键按下
            {
                isDragging = true;
                lastTouchPosition = Input.mousePosition;
                y=tkcGame.player.cameraRoot.rotation.eulerAngles.x;
                x=tkcGame.player.cameraRoot.rotation.eulerAngles.y;
            
            }
            else if (Input.GetMouseButton(0) && isDragging) // 鼠标左键拖动
            {
                Vector2 currentMousePosition = Input.mousePosition;
                Vector2 delta = currentMousePosition - lastTouchPosition;

                x += delta.x * mouseSensitivity; // 水平旋转
                y -= delta.y * mouseSensitivity;

                y = Mathf.Clamp(y, 0, 70);
                // y = Mathf.Clamp(y, 0, 70);
            
                tkcGame.player.cameraRoot.transform.rotation = Quaternion.Euler(y, x, 0.0f);
            
                lastTouchPosition = currentMousePosition;
            }
            else if (Input.GetMouseButtonUp(0)) // 鼠标左键抬起
            {
                isDragging = false;
            }
#else // 移动设备中的触摸旋转逻辑
        // 检测触摸输入
        Touch touch = Input.GetTouch(0);

        if (touch.phase == TouchPhase.Began)
        {
            // 开始触摸
            isDragging = true;
            lastTouchPosition = touch.position;
            y=tkcGame.player.cameraRoot.rotation.eulerAngles.x;
            x=tkcGame.player.cameraRoot.rotation.eulerAngles.y;
        }
        else if (touch.phase == TouchPhase.Moved && isDragging)
        {
            // 计算滑动增量
            Vector2 delta = touch.deltaPosition;
            x += delta.x * mouseSensitivity; // 水平旋转
            y -= delta.y * mouseSensitivity;

            y = Mathf.Clamp(y, 0, 70);
            // y = Mathf.Clamp(y, 0, 70);
            
            tkcGame.player.cameraRoot.transform.rotation = Quaternion.Euler(y, x, 0.0f);
        }
        else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
        {
            // 结束触摸
            isDragging = false;
        }
#endif
        }

        #endregion
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值