Unity 位置计算
问题:获取某个位置的坐标在前方(或后方)一定距离的方法
public Vector3 GetPosition(Quaternion rotation, Vector3 position, float distance)
{
Vector3 direction = rotation * Vector3.forward;
return position + (direction * distance);
}
Unity3D是如何计算 forward的
public Vector3 forward
{
get
{
return this.rotation * Vector3.forward;
}
set
{
this.rotation = Quaternion.LookRotation(value);
}
}
Ray结构及其GetPoint函数用于通过提供起始点,方向和距离来计算未知距离找到
Vector3 startingPostion = new Vector3(4, 6, 2);
Quaternion theDirection = Quaternion.identity;
Ray ray = new Ray();
//Set the starting point
ray.origin = startingPostion;
//Set the direction
ray.direction = Vector3.forward + theDirection.eulerAngles;
//Get the distance
ray.GetPoint(100);
或者
Ray ray = new Ray(startingPostion, worldRotation * Vector3.forward);
//Get the distance
ray.GetPoint(100);
参考链接:https://www.pianshen.com/article/3402173540/