Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

getting the normal of a plane

Discussion in 'Scripting' started by Juxtaposed, Dec 8, 2019.

  1. Juxtaposed

    Juxtaposed

    Joined:
    Aug 23, 2015
    Posts:
    29
    I am running on Android. I have a program where I place a plane on the floor in an AR application at the location of a placement indicator (placement indicator follows AR planes and is placed based on a raycast from the center of screen)

    Since I cannot guarantee that the plane will be placed with no rotation I can't guarantee that a Vector3.up is the normal of the place. How do I get the normal to the plane?

    Code (CSharp):
    1. placedPlane = GameObject.CreatePrimitive(PrimitiveType.Plane);
    2. placedPlane.transform.SetPositionAndRotation(placementIndicator.transform.position, placementIndicator.transform.rotation);
    3. debugLog("placed plane's rotation: " + placedPlane.transform.rotation);
    4. Plane planeComponent = placedPlane.GetComponent<Plane>();
    5. Vector3 normal = new Vector3(planeComponent.normal.x, planeComponent.normal.y, planeComponent.normal.z);
    6. debugLog("plane's normal vector: " + normal.ToString());
    I have created a debug "console window" at the bottom of the screen. debugLog(string) simply add that text to a new line in its scrollview.

    The first debug log reports back a non zero rotation, which is not surprising because AR planes are not going to be perfect.

    The second debug log gives a (0,0,0) value for the normal vector always.

    An example output is:
    placed plan's rotation: (0.0, -0.9, 0.0, -0.4)
    normal vector: (0.0, 0.0, 0.0)

    My guess is that I am grabbing the Plane component incorrectly, because a normal vector should at least have some value somewhere. And as far as I can tell you can't check if you failed to get a Plane object with getComponent because you can't check a Plane object == null.

    How should I get the normal vector of a plane?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Bingo! So a UnityEngine.Plane is not a component that can be .GetComponent-ed. It is a purely mathematical construct, a "Representation of a plane in 3D space," as the Unity docs say when you hover over it.

    A UnityEngine.Plane is also a struct, not a class, which is also why it can't be compared to null.

    When you GameObject.CreatePrimitive a PrimitiveType.Plane, that makes a GameObject with a MeshRenderer, MeshFilter, the flat plane mesh, etc. There is no actual UnityEngine.Plane structure per se to be gotten and the two are completely unrelated.

    However, given that we know it's a flat mesh that Unity provides, the normal to it will always be transform.up.

    transform.up is different from Vector3.up, which is a constant unchanging (0,1,0).

    transform.up is the local "up" for any given object at its current transform, and since we know the plane mesh is flat, that's the normal too, coincidentally.
     
    Vorrin and CORMAC134 like this.
  3. Juxtaposed

    Juxtaposed

    Joined:
    Aug 23, 2015
    Posts:
    29
    Thanks, this cleared things up quite a bit.
     
    Kurt-Dekker likes this.