Unity基础向量相关

目录

1.向量

 1 位置:代表一个点

 2 方向:代表一个方向

2.两点决定——向量

3.零向量和负向量

4.向量的模长

5.单位向量(主要是用来进行移动计算的)


1.向量

三维向量Vector3,Vector3有两种几何意义:

 1 位置:代表一个点

print(this.transform.position);

 2 方向:代表一个方向

print(this.transform.forward);

print(this.transform.up);

Vector3 v=new Vector3(1,2,3);

Vector2 v2=new Vector2(1,2);

2.两点决定——向量

终点减起点。

假设A和B的几何意义是两个点:

Vector3 A=new Vector3(1,2,3);

Vector3 B=new Vector3(5,1,5);

求向量:

此时AB和BA是两个向量。

Vector3 AB=B-A;
Vector3 BA=A-B;

3.零向量和负向量

print(Vector3.zero);
print(Vector3.forward);
print(-Vector3.forward);

4.向量的模长

Vector3中提供了获取向量模长的成员属性。

magnitude

print(AB.magnitude);

或者:

//求出向量的长度
Vector3 C=new Vector3(5,6,7);
print(C.magnitude);
//求两点之间的长度
Vector3.Distance(A,B);

5.单位向量(主要是用来进行移动计算的)

normalized是Vector3中提供的获取单位向量的成员属性。

print(AB.normalized);

print(AB/AB.magnitude);

对于一个向量A :

A(x,y,z)

单位向量=(x/模长+y/模长+z/模长)

猜你喜欢

转载自blog.csdn.net/2303_76354097/article/details/133893444