向量的模长和方向

向量的大小:向量各分量的平方和的平方根
在这里插入图片描述
API:float dis=vector.magnitude

代码实现:

  //模长
    void Demo01()
    {
        Vector3 pos = this.transform.position;

        Debug.DrawLine(Vector3.zero,this.transform.position);
        //利用数学公式求模长
        float m1 =Mathf.Sqrt( Mathf.Pow(pos.x,2)+Mathf.Pow(pos.y,2)+Mathf.Pow(pos.z,2));
        //利用Unity封装的方法(API)求模长
        float m2 = pos.magnitude;
        //利用距离公式求模长
        float m3 = Vector3.Distance(Vector3.zero,pos);

        Debug.LogFormat("{0},{1},{2}",m1,m2,m3);
    }

向量的方向:
获取向量也称为“标准化向量”或“归一化向量”,即获取该向量的单位向量

单位向量:大小为1的向量

几何意义:将该向量拉长或缩短,使模长等于1

公式:V/|V|

vector2为vector1的单位向量
vector1.Normalized();将vector自身设置为单位向量

API:Vector3 Vector2=vector1.normalized;

代码实现:

 //方向
    void Demo02()
    {
        Vector3 pos = this.transform.position;
        Debug.DrawLine(Vector3.zero, this.transform.position);
        //数学公式
        Vector3 dir1 = pos / pos.magnitude;
        //UnityAPI 封装好的方法
        Vector3 dir2 = pos.normalized;
        Debug.DrawLine(Vector3.zero,dir1,Color.red);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值