点乘、叉乘的作用

点乘判断角度
叉乘获得方向

  [ContextMenu("Test")]
   public void Test()
    {
        //---点乘。判断角度------------------------------------
        Debug.Log("点乘");
        Debug.Log(Vector3.Dot(_A.right, (_B.position - _A.position)));//正,在前,负,在后

        Debug.Log(Vector3.Angle(_A.right, (_B.position - _A.position)));//角度

        Debug.Log(Mathf.Acos(Vector3.Dot(_A.right.normalized, (_B.position - _A.position).normalized)) * Mathf.Rad2Deg);//角度


        //----叉乘。判断方向----------------------
        //叉乘:两个向量相乘 ,得到一个新的向量,新向量垂直于之外的两个向量
        Debug.Log("叉乘");
        Debug.Log(Vector3.Cross(_A.right, (_B.position - _A.position)));
        //角度
        Debug.Log( Vector3.Cross(_A.right, (_B.position.normalized - _A.position).normalized));
        Debug.Log(Vector3.Distance(Vector3.zero, Vector3.Cross(_A.right, (_B.position.normalized - _A.position).normalized)));
        Debug.Log(Mathf.Asin( Vector3.Distance(Vector3.zero, Vector3.Cross(_A.right, (_B.position.normalized - _A.position).normalized))));
        Debug.Log((Mathf.Asin(Vector3.Distance(Vector3.zero, Vector3.Cross(_A.right, (_B.position.normalized - _A.position).normalized))))*Mathf.Rad2Deg);

        //方向
        if(Vector3.Cross(_A.right, (_B.position - _A.position)).y > 0)
        {
            //顺时针
        }
        if (Vector3.Cross(_A.right, (_B.position - _A.position)).y == 0)
        {
            //正方向平行
        }
        if (Vector3.Cross(_A.right, (_B.position - _A.position)).y < 0)
        {
            //逆时针
        }

        //角度
        Vector3 c = Vector3.Cross(_A.right, (_B.position - _A.position));
        float angle = Vector3.Angle(_A.right, (_B.position - _A.position));

        //b-a的夹角
        float basign = Mathf.Sign(Vector3.Dot(c.normalized,Vector3.Cross(_A.right.normalized, (_B.position - _A.position).normalized)));
        Debug.Log("b--a"+ basign * angle);

        float absign = Mathf.Sign(Vector3.Dot(c.normalized, Vector3.Cross(_A.right.normalized, (_A.position - _B.position).normalized)));
        Debug.Log("a--b" + absign * angle);
    }

你可能感兴趣的:(点乘、叉乘的作用)