Unity C#向量数学

rhymo
rhymo
管理员
1133
文章
0
粉丝
游戏素材评论1,719阅读模式

Unity C#向量数学

Vector3 a = new Vector3(2, 2, 0);//定义一个三维向量
float length = a.magnitude;//计算a的长度
Debug.Log("长度:" + length);
Vector3 b = a.normalized;//把a变为单位向量
Debug.Log("标准化为:" + b.ToString("F3"));//保留三位小数
 
//x轴正方向向右
//y轴正方向向上
//z轴正方向向前
 
//单位向量
Vector3 c = Vector3.right ;//(1,0,0)
c = Vector3.up ;//(0,1,0)
c = Vector3.forward ;//(0,0,1)
c = a + b;//加法
Debug.Log("c:" + c);//输出c
c = a * 2;//乘法
float d = Vector3.Dot(a, b);//点乘结果为小数,不能再与向量运算
c = Vector3.Cross(a, b);//叉乘,结果仍是向量
 
//计算两个游戏对象间的距离
GameObject target = GameObject.Find("自己填");
Vector3 p2 = target.transform.position;
Vector3 p1 = this.transform.position;
Vector3 direction = p2 - p1;
Debug.Log("物体间的距离:" + direction.magnitude);

 
rhymo
  • 本文由 rhymo 发表于2023-04-30 07:33:51
  • 转载请务必保留本文链接:https://www.artcg.design/1484.html
匿名

发表评论

匿名网友
确定

拖动滑块以完成验证