树洞中的狐狸 2022-03-14 20:32 采纳率: 100%
浏览 24
已结题

关于拖动旋转的一些疑问

在网上看到一个鼠标拖动旋转的功能实现

public void OnBeginDrag(PointerEventData data)
        {
            if (!isActive)
            {
                return;
            }

            isSpinning = true;

            playerController.StopMoving();

            // get the angle to the mouse position on down frame
            Vector3 inputPosition = new Vector3(data.position.x, data.position.y, 0f);
            directionToMouse = inputPosition - Camera.main.WorldToScreenPoint(pivot.position);

            // store the angle to mouse pointer on down frame
            previousAngleToMouse = Mathf.Atan2(directionToMouse.y, directionToMouse.x) * Mathf.Rad2Deg;

        }

public void OnDrag(PointerEventData data)
        {
            if (isSpinning && Camera.main != null && pivot != null && isActive)
            {
                // get the angle to the current mouse position
                Vector3 inputPosition = new Vector3(data.position.x, data.position.y, 0f);
                directionToMouse = inputPosition - Camera.main.WorldToScreenPoint(pivot.position);
                angleToMouse = Mathf.Atan2(directionToMouse.y, directionToMouse.x) * Mathf.Rad2Deg;

                // if we have dragged a minimum threshold, rotate the target to follow the mouse movements around the pivot
                // (left-handed coordinate system; positive rotations are clockwise)
                if (directionToMouse.magnitude > minDragDist)
                {
                    Vector3 newRotationVector = (previousAngleToMouse - angleToMouse) * axisDirection;
                    targetToSpin.Rotate(newRotationVector);
                    previousAngleToMouse = angleToMouse;
                }
            }
        }

代码是在拖动开始时通过求鼠标点击位置与旋转支点的XY求tanY/X得到当前鼠标的角度,然后在拖动继续时求新的角度。我没有理解的地方是为什么使用原来的角度减去新的角度,旋转的时候角度不是在不断变大吗,这样的话传入Rotate的值就是一个负值,但游戏中旋转是正常的,并且不管怎么旋转始终是在0-359度之间。是我理解错误了吗?

img

  • 写回答

2条回答 默认 最新

  • 小鱼游戏开发 2022-03-14 22:50
    关注

    Rotate(angle) 当前角度旋转 angle度
    angleToMouse求出来仅是基于0的角度
    需要使用旧的 - 新的 得到偏移角度

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月23日
  • 已采纳回答 3月15日
  • 创建了问题 3月14日

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题