Unity向量的乘积

366 阅读1分钟

Unity 向量乘积的基本概念

image.png

 void Update()
    {
        #region 补充知识 调试画线
        //画线段 
        //前两个参数 分别是 起点 终点
        //Debug.DrawLine(this.transform.position, this.transform.position + this.transform.forward, Color.red);
        //画射线
        //前两个参数 分别是 起点 方向
        //Debug.DrawRay(this.transform.position, this.transform.forward, Color.white);
        #endregion

        #region 知识点一 通过点乘判断对象方位
        //Vector3 提供了计算点乘的方法
        Debug.DrawRay(this.transform.position, this.transform.forward, Color.red);
        Debug.DrawRay(this.transform.position, target.position - this.transform.position, Color.red);
        //得到两个向量的点乘结果
        //向量 a 点乘 AB 的结果
        float dotResult = Vector3.Dot(this.transform.forward, target.position - this.transform.position);
        if( dotResult >= 0 )
        {
            print("它在我前方");
        }
        else
        {
            print("它在我后方");
        }
        #endregion

        #region 知识点二 通过点乘推导公式算出夹角
        //步骤
        //1.用单位向量算出点乘结果
        dotResult = Vector3.Dot(this.transform.forward, (target.position - this.transform.position).normalized);
        //2.用反三角函数得出角度
        print("角度-" + Mathf.Acos(dotResult) * Mathf.Rad2Deg);

        //Vector3中提供了 得到两个向量之间夹角的方法 
        print("角度2-" + Vector3.Angle(this.transform.forward, target.position - this.transform.position));
        #endregion
    }

运用实例:

image.png

脚本代码

public class jiaoben : MonoBehaviour
{
    public Transform Players;//获取跟随物体的坐标值
    private float diancheng;//记录点乘值
    private Vector3 xiangliang;//
    private float juli;//记录两者之间的距离
    void Update()
    {
        //获得AB向量
        xiangliang = (Players.position - this.transform.position).normalized;
        //运用Distance函数求出两点之间的距离
        juli = Vector3.Distance(this.transform.position, Players.position);
        //点乘--其中计算的是方向向量"
        diancheng = Vector3.Dot(this.transform.forward, xiangliang);//Vector3.Dot()"
        //通过反余弦函数算出夹角
        var jiaodu = Mathf.Acos(diancheng) * Mathf.Rad2Deg;
        // 输出判断,当距离小于五并且角度小于22.5时
        if (jiaodu > 22.5 && juli <= 5)
        {
            print("发现入侵者");
        }
        ///
         //计算两者之间的距离是否小于等于5
        if (Vector3.Distance(this.transform.position,Players.position)<=5)
        {
            //计算出点乘的结果(方向向量)
            diancheng = Vector3.Dot(this.transform.forward, (Players.position - this.transform.position).normalized);
            //通过反余弦函数 算出夹角
            if (Mathf.Acos(diancheng)*Mathf.Rad2Deg<=22.5f)
            {
                print("发现入侵者");
            }
        }
    
        //直接得出两个函数之间的夹角
        diancheng = Vector3.Angle(this.transform.forward,Players.position-this.transform.position);
        juli = Vector3.Distance(Players.position,this.transform.position);
        if (diancheng<=22.5&&juli<=5)
        {
            print("发现入侵者");
        }
    }
}
    }

函数注解:

1.Vector3.normalized()函数

image.png

2.Vector3.DOt()函数

image.png

3.Vector3.Distance()函数

image.png

4.Vecro3.Angle()函数

image.png