Detecting Plane Intersection

I’m having trouble detecting a plane where I don’t want a prefab to generate. I’ve tried using Physics.Raycast but that will prevent the generation of the prefab on the path to the plane and not the plane itself.(See attached image).

This is the code I’m using to detect the plane.

                RaycastHit hit;
                if (Physics.Raycast(currentPoint, currentPoint, out hit))
                //if (false)
                {
                    var gameObjectCollision = hit.collider.gameObject;

                    if (!gameObjectCollision.CompareTag("NonGrassSpace"))
                        AddRandomGrassSpace(currentPoint);
                }
                else
                {
                    AddRandomGrassSpace(currentPoint);
                }

I’m wondering if I should be using a different library to detect the plane, but I haven’t found anything else to do this yet. Any suggestions would be greatly appreciated!

Hey,

“Physics.Raycast(currentPoint, currentPoint, out hit)”
Isn’t that a typo? You set “currentPoint” for both the origin and the direction of the raycast.

I could use more info, especially where do you cast the ray from, what the “currentPoint” is and how it’s calculated.
I might misunderstand, but somehow you already have each points where to add grass?
The usual standard in “projecting” object onto a surface that’s horizontal in world space (“the ground”) is to do something like this:

// This is assuming the plane's pivot is exactly in its middle, like it's the case with all Unity's primitive shapes.
// The projection is done from the "sky" towards the "ground"

// If you don't know how to get this, use: <plane's own position> + Vector3.up * <<plane's thickness>/2 + .1f>.
Vector3 startingPoint = <position at plane's surface> + Vector3.up;
// These depend on your level's size.
float coveringAreaHor = 100f;
float coveringAreaVert = 100f;
// Spacing between the projected objects
float spacingBetweenObjects = .2f;

Vector3 lowerLeftPosition = startingPoint - new Vector3(coveringAreaHor, 0, coveringAreaVert)/2;

int numInstancesHor = (int)(coveringAreaHor / spacingBetweenObjects);
int numInstancesVert = (int)(coveringAreaVert / spacingBetweenObjects);

for (int i = 0; i < numInstancesHor; ++i)
{
    float x = spacingBetweenObjects * i;
    for (int j = 0; j < numInstancesVert; ++j)
    {
        var origin = lowerLeftPosition + new Vector3(x, 0f, spacingBetweenObjects * j);
        if (Physics.Raycast(origin, Vector3.down, out hit))
        {
            var gameObjectCollision = hit.collider.gameObject;
            if (!gameObjectCollision.CompareTag("NonGrassSpace"))
                AddRandomGrass(hit.point); // use the "point" property to get the exact collision point where to "plant" the grass
        }
    }
}

If this is not what you needed, please provide more info, especially where do you cast the ray from, what the “currentPoint” is and how it’s calculated.

– Lucian

1 Like

Hey Lucian, I really appreciate your feedback! I have similar code to what you have shown with the nested for loops when projecting out objects onto my surface. I think the Vector3.down as the second param (as you pointed out) in the Physics.Raycast method might be the source of my issue. I’m going to try that and reply back with my results and the rest of my relevant code if I still have issues.

Thanks again for your help!

1 Like

I just wanted to check back in and let you know the recommendation worked as expected. I appreciate the help and quick response!

1 Like