How to interact with the detected plane?

I want to place objects at random locations within the bounds of a detected plane via a Spawner script. My problem is that I am not being able to figure out a way to make sure that the locations from where the objects are spawned always takes place at any random location within the detected plane surface.
For eg) If I scan a ground plane I want game objects to spawn on that ground plane
How can I utilize the WorldInterpreter script and the Floor game object to do so?

My Spawner Script -

public class Spawner : MonoBehaviour
{
    [SerializeField]
    private GameObject myPrefab;
    public float minWait;
    public float maxWait;
    public float offset = 3;
    private bool isSpawning;
 
    void Awake()
    {
        isSpawning = false;
    }
 
    void Update()
    {
        if (!isSpawning)
        {
            float timer = Random.Range(minWait, maxWait);
            Invoke("SpawnObject", timer);
            isSpawning = true;
        }
    }
 
    void SpawnObject()
    {
        float randX = Random.Range(-offset,offset);
        float randZ = Random.Range(-offset,offset);
      
        Instantiate(myPrefab,new Vector3(randX,transform.position.y,randZ),Quaternion.identity);
        isSpawning = false;
    }
}

Hi Rajat,

Thanks for posting! Below are my thoughts on how you might solve this.

If you want objects to spawn on all AR planes that you scan, do the following in Unity:

:one: - In the Project window, copy Packages / Litho Beta SDK / Core / Prefabs / AR / ARPlane into your Assets folder - let’s call this copy Assets / SpawnerARPlane

:two: - In the Hierarchy window, select ARSessionOrigin

:three: - In the Inspector window, look for the component AR Plane Manager on the ARSessionOrigin object - this will have a Plane Prefab variable

:four: - Drag your new SpawnerARPlane prefab from the Project window to the Inspector window, and drop it into the Plane Prefab field of the AR Plane Manager component

:five: - From the Project window, open your new SpawnerARPlane prefab for editing by double-clicking it

:six: - Attach your Spawner script to the SpawnerARPlane prefab using the “Add Component” button in the Inspector window

:point_right: - Now when a plane is detected, SpawnerARPlane instances will be used to represent it, meaning each plane will also now have the Spawner script attached to it, and hence will start spawning objects on itself.

:bulb: - To ensure the spawned objects only appear within the bounds of the AR plane, you can use GetComponent<ARPlane>().boundary in your Spawner script to retrieve a list of Vector2 objects which describe the perimeter of the plane. You can then find the minimum and maximum x and y values by looping through all of those boundary points - use these min and max values for generating the random point.

:bulb: - To make it more accurate, you can use an algorithm to check whether the generated random point is within the boundary, and generate a new one if it is not. Such an algorithm can be found from other sources, such as this one on StackOverflow: https://stackoverflow.com/a/14998816 (you’ll need to replace PointF with Vector2).

:exclamation: - Note that it is recommended that you don’t change any of the scripts provided in the Litho Beta SDK package (e.g. WorldInterpreter) - they are accessible for modification, however any changes you do make may cause future package updates to break your project (as your code modifications will get replaced by the code in the updated package).

Excited to see what you’re making - let us know how you get on!

2 Likes

Thanks a lot for the detailed response. I think I will be able to make what I have in my mind now. This actually helps! I will definitely post whatever I make on social media :slight_smile:

2 Likes

2 posts were merged into an existing topic: How do I view Debug.Log messages on mobile builds?

No worries, glad to help :slight_smile:

(I have migrated your subsequent question to a new topic, as this makes it easier for other people to find it)

1 Like