方位与向量的运算

已知GameObject  _Go,初始点的朝向是面向Vector3.forward(所谓初始点朝向即transform.localRotation = Quaternion.Euler(Vector3.zero)时),获得当前朝向按Y轴旋转的方向向量:

Vector3 dir= _Go.transform.localRotation * Vector3.forward;

获得当前朝向按Z轴和X轴旋转的方向向量:

Vector3 dir= _Go.transform.localRotation * Vector3.up;

获得当前朝向向量的四元数:

Vector3 dir= _Go.transform.localRotation * Vector3.forward;

Quaternion qua = Quaternion.LookRotation(dir);

或者(x、y、z同时同步)

Vector3 dir1= _Go.transform.localRotation * Vector3.forward;

Vector3 dir2= _Go.transform.localRotation * Vector3.up;

Quaternion qua = Quaternion.LookRotation(dir,dir2);

计算当前朝向的前方距离10米处:

Vector3 loc = _Go.transform.rotation * Vector3.forward;

loc =  _Go.transform.position + loc.normalized * 10;

计算当前朝向的后方距离10米处:

Vector3 back10 = _Go.transform.rotation.eulerAngles;

back10= back10 + new Vector3(0, 180, 0);

Quaternion qua = Quaternion.Euler(back10);

back10 = qua * Vector3.forward;

back10 = vec.normalized * 10 + _Go.transform.position;

其他方向的计算同理。

计算在当前的基础上旋转x,y,z度:

_Go.transform.localRotation = _Go.transform.localRotation * Quaternion.Euler(new Vector3(x,y,z));

你可能感兴趣的:(方位与向量的运算)