Unity判断当前鼠标顺逆时针旋转的简单算法

</pre><pre name="code" class="csharp">    private bool _judge;

    private Vector2 _anchor;<span style="white-space:pre">	</span>//偏移的圆心
    private Vector2 _lastPoint;  //上一帧座标

    void Update()
    {
        if (Input.GetMouseButtonUp(0))
        {
            _judge = false;
        }

        if (Input.GetMouseButtonDown(0))
        {
            _lastPoint = Input.mousePosition;
            _anchor = _lastPoint - new Vector2(0, 50);<span style="white-space:pre">	</span>//鼠标点击 在点击位置下方设置偏移一定数值的圆心(具体偏移量看实际运用
            _judge = true;
        }

        if (_judge)
        {
            Debug.Log(TouchJudge(Input.mousePosition, ref _lastPoint, _anchor));
        }
    }

    
    /// <summary>
    /// 判断顺时针逆时针
    /// (顺正逆负
    /// </summary>
    /// <param name="current">当前座标</param>
    /// <param name="last">上个座标(ref 更新</param>
    /// <param name="anchor">锚点</param>
    /// <returns></returns>
    private float TouchJudge(Vector2 current,ref Vector2 last,Vector2 anchor)
    {
        Vector2 lastDir = (last - anchor).normalized;  <span style="white-space:pre">		</span>//上次方向
        Vector2 currentDir = (current - anchor).normalized;   <span style="white-space:pre">	</span>//当前方向

        float lastDot = Vector2.Dot(Vector2.right, lastDir);
        float currentDot = Vector2.Dot(Vector2.right, currentDir);

        float lastAngle = last.y < anchor.y<span style="white-space:pre">		</span>//用y点判断上下扇面
            ? Mathf.Acos(lastDot)*Mathf.Rad2Deg
            : -Mathf.Acos(lastDot)*Mathf.Rad2Deg;

        float currentAngle = current.y < anchor.y
            ? Mathf.Acos(currentDot) * Mathf.Rad2Deg
            : -Mathf.Acos(currentDot) * Mathf.Rad2Deg;

        last = current;
        return currentAngle - lastAngle;
    }

点赞