Unity判断物体正在顺时针或逆时针旋转

  private Vector3 forward;

    void Update()
    {
        forward = transform.forward;

        Debug.DrawLine(transform.position, forward, Color.red);

        if (Input.GetKey(KeyCode.Q))
        {
            transform.Rotate(0, 90 * Time.deltaTime, 0);
        }
        else if (Input.GetKey(KeyCode.E))
        {
            transform.Rotate(0, -90 * Time.deltaTime, 0);

        }
    }

    private void LateUpdate()
    {
        Vector2 v1 = new Vector2(forward.x, forward.z); //旋转前的前方
        Vector2 v2 = new Vector2(transform.forward.x, transform.forward.z); //旋转后的前方
        Debug.DrawLine(transform.position, transform.forward, Color.blue);

        float rightFloat = v1.x * v2.y - v2.x * v1.y ;

        if (rightFloat < 0)
        {
            Debug.Log("向右转了");
        }
        else if(rightFloat >0)
        {
            Debug.Log("向左转了");
        }
        else
        {
            Debug.Log("没转");
        }

    }

向量叉乘

X1Y2-X2Y1>0  b在a 的左侧  ;X1Y2-X2Y1<0 在a 的右侧

你可能感兴趣的:(游戏开发,unity,C#脚本,C#,unity,游戏引擎)