如何获取相对于任意旋转的平面的 xy 轴的坐标



给定平面上具有任意旋转的随机点,假设我将平面上的任意点称为原点,我如何获得该点相对于平面上xy轴的xy坐标?我可以在世界空间中获得该点的绝对 xyz 坐标(使用光线投射(,但相对坐标让我难倒了。

让我们看看现在我是否正确理解了它。您可以使用来自摄像机的光线投射来击中飞机上的某个点。光线投射返回一个世界位置,但您想要一个局部位置(并且原点不同于 0,0?InverseTransformPoint可以做到这一点。 下面的代码使用鼠标选择一个屏幕点并创建光线投射,以获取命中点相对于命中对象的本地位置。

if(Input.GetMouseButtonDown(0))
{
Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(r,out hit))
{
Debug.Log(hit.transform.InverseTransformPoint(hit.point)); // point relative to local (0,0)
Debug.Log(hit.transform.InverseTransformPoint(hit.point- new Vector3(0.5f,0.5f,0)));// local point with origin as local (0.5,0.5)
}
}

[旧答案]

如果我正确理解了你的问题,你想得到相对于该平面中局部位置的世界位置。为此,您可以使用平面的变换。Transform.TransformPoint 方法可用于将局部位置转换为世界位置。

下面的示例使用相对于对象的局部位置,并在 update 方法中获取世界位置。如果您在游戏运行时旋转飞机,它将相对于本地位置更改世界位置。

using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
public Vector3 localPos;
public Vector3 worldPos;
void Update()
{
worldPos = transform.TransformPoint(localPos);
}
}

最新更新