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

    private bool _judge;

    private Vector2 _anchor;	//偏移的圆心
    private Vector2 _lastPoint;  //上一帧坐标

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

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

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

    
    /// 
    /// 判断顺时针逆时针
    /// (顺正逆负
    /// 
    /// 当前坐标
    /// 上个坐标(ref 更新
    /// 锚点
    /// 
    private float TouchJudge(Vector2 current,ref Vector2 last,Vector2 anchor)
    {
        Vector2 lastDir = (last - anchor).normalized;  		//上次方向
        Vector2 currentDir = (current - anchor).normalized;   	//当前方向

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

        float lastAngle = last.y < anchor.y		//用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;
    }



你可能感兴趣的:(算法)