- Home /
Assigning plane script to game object plane
if you create a plane using this planeA.Set3Points(Vector1, Vector2, Vector3); How can you assign it to a gameobject so you can see it?
Answer by Bunny83 · Jan 19, 2018 at 02:35 AM
If you talk about Unity's Plane struct you got something completely wrong. It represents an infinite mathematical plane. This has nothing to do with a visual plane or Mesh. The plane can be used to calculate intersections with rays, test if a point is on the front or backside of the plane and to project a point onto the plane.
Since you haven't said what you actually want to achieve I can't suggest anything else.
@bunny83 Yes i'm looking for a way to visually see the plane im creating using plane struct
That's not possible. The plane is a mathematical concept. It doesn't have a border. It's an infinite plane. It doesn't have a shape. It doesn't even have a "location", just a distance from the origin. It represents a two dimensional space. To render something visually it has to be finite and need to be broken down to triangles and therefore needs a border.
The plane is just defined by 4 numbers in point-normal form. a, b, c represent the normal vector of the plane and d is the distance of the plane from the origin. Every point (x,y,z) that satisfies the equation
a*x +b*y +c*z + d = 0
is on the plane.
If you just want to roughly visualize the orientation of the plane you can draw the triangle that you used to create the plane. This triangle lays in the plane. So it represents a finite part of the otherwise infinite plane.
Answer by CasualT_Bossfight · Feb 24, 2020 at 11:49 PM
I was looking for a way to visualize a Plane in Unity and came across this question and was disappointed there was no answer. So I had to figure it out myself and here's what I came up with. This took me awhile cause I have a learning disability when it comes to Quaternions ;)
// Construct your plane using your values
Vector3 normal, point;
Plane plane = new Plane(normal, point);
GameObject planeGO = GameObject.CreatePrimitive(PrimitiveType.Plane);
planeGO.transform.localScale = new Vector3(1000, 1000, 1000); // A plane is infinitely large, so use any scaling values here which make sense for you
// For Unity's default plane GameObject the Y axis is the plane's normal. So we need a perpendicular to the plane's normal.
Vector3 perpendicular;
if (Vector3.Angle(plane.normal, Vector3.up) > 5)
{
perpendicular = Vector3.Cross(plane.normal, Vector3.up);
}
else
{
perpendicular = Vector3.Cross(plane.normal, Vector3.right);
}
Quaternion planeRotation = new Quaternion();
planeRotation.SetLookRotation(perpendicular, plane.normal);
planeGO.transform.rotation = planeRotation;
// If you have the point used to construct the Plane, just use that as the GO's position. If you don't, then any point in the plane will do.
if (haveAccessToPlanePoint)
{
planeGO.transform.position = point;
}
else
{
Vector3 planePoint = plane.ClosestPointOnPlane(Vector3.zero);
planeGO.transform.position = planePoint;
}