Search code examples
androidandroid-studioaugmented-realityarcoresceneform

How to place objects on a Sceneform plane randomly?


I want to build an AR game using Sceneform where an object is placed on the plane on a timer (ex: every 1 second, an object is placed). I also want it to be placed at a random position on the detected plane.

I only know how to place an object using a HitTest, but that places it only where and when the user taps. I want the placement to be controlled by the app as this is a game.


Solution

  • When you detected the plane, you need create an anchor at center point(or random position) in the plane.

    DetectedPlane planeDetected = _newPlanes[0]
    Anchor anchor = plane.CreateAnchor(plane.CenterPose);
    

    After, use anchor transform to render object in environment.

    I am not an Android developer, I use ARCore with Unity, but I think it will be the same solution.