'I want to create an object as soon as ARCore recognizes a plane

In the ARCore tutorial, recognizing a plane and touching it creates an object on the screen. But I want to create an object as soon as I know the plane.

The algorithm process Recognize the plane -> Touch the recognized plane -> The object is created in the touched plane. Here, the object is generated from the recognized plane. I want to change this.

I have no idea what part to modify in this ARCore tutorial. Please help me.... Thanks.



Solution 1:[1]

You can create the object at the location your camera is "looking" at. Therefore, as soon as the plane is detected and your camera is pointing at it, you create the object.

This can be done using Raycasting (Assuming you use Unity since you used C# tag). Just shoot a ray from your camera and check if you are hitting a detected plane. If you are, just create the object at that location

keep a global reference to the create object so you can create it only once

GameObject spawnedObject;

Then do the raycasting

RaycastHit hit;
Ray spawnRay = ARCamera.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
LayerMask selectLayers = 1 << LayerMask.NameToLayer(DetectedPlanesLayer);
if (spawnedObject == null && Physics.Raycast(spawnRay, out hit, Mathf.Infinity, spawnLayers))
{
    spawnedObject = Instantiate(objectPrefab, hit.point, Quaternion.identity);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1