从 UnityEngine.GameObject 平面创建 UnityEngine.Plane

Create UnityEngine.Plane from UnityEngine.GameObject Plane

也许很简单,但我做不对。 我在 Editor -> 3D-Object -> Plane 中创建了一个平面。 用户可以与该平面交互。

但在代码中我需要 UnityEngine.Plane,而不是 UnityEngine.Gameobject

我了解到我不能这样做 GetComponent<>() 因为 Plane 是一个结构,而不是一个组件。

但是如何将我拥有的游戏对象平面转换为 UnityEngine.Plane 或用它创建一个新平面?

我找到了 this,但我不知道这对我有什么帮助。

我想你误解了 UnityEngine.Plane 是什么。

来自 Unity Documentation :

Representation of a plane in 3D space.

就像 Vector3 或 Quaternion 一样,这不是原始网格,也根本不是 3D 网格。您不能将 Plane GameObject 转换为该结构。

您可以做的是创建平面网格的表示以对其进行一些计算。但是你不能用它来实例化一个新的平面。

如果您想创建一个代表您的平面游戏对象的平面,您可以使用您的平面游戏对象的 MeshRendererMeshFilter 中的一些数据调用 UnityEngine.Plane 的构造函数。

var filter = GetComponent<MeshFilter>();
Vector3 normal;

if(filter && filter.mesh.normals.Length > 0)
    normal = filter.transform.TransformDirection(filter.mesh.normals[0]);

var plane = new Plane(normal, transform.position);

这应该为您提供平面游戏对象的平面表示,朝相同的方向看并穿过平面的位置。

你是对的,原始平面与结构平面不是一回事。

要创建 UnityEngine.Plane,请使用其构造函数 https://docs.unity3d.com/ScriptReference/Plane-ctor.html

最简单的情况是这样

Plane plane=new Plane(Vector3.up, Vector3.zero);

Up表示平面的法向量(这里我假设你希望平面是水平的,所以'looking up',第二个向量是平面必须通过的点(在这个案例零,因为我假设你希望飞机与地面水平)。