Hi everyone,
I want to make an AR experience where it just continuously drops a cube into the scene every 10 seconds. I was able to create something close enough in Reality Composer where it spawns a cube every time every time you tap the screen but a) it's not automatic and b) it's not continuous. Basically I am moving a cube from very far away so it cannot be seen into the view and then dropping it. Instead I would like this action to be automated and continuous on a loop.
Any ideas on how to archive this?
Many thanks in advance! – Berke
You would need to create new cubes programmatically, which you can do either by cloning an existing entity, or by creating an entirely new object. If you want spawn multiple copies of the same object, cloning is more efficient, since RealityKit can share resources between instances.
The steps involved are:
- Retrieve the cube entity from the loaded scene.
if let boxScene = try? Experience.loadBox() {
if let box = boxScene.findEntity(named: "Steel Box") {}
...
}
}
- Clone the entity.
let boxCopy = box.clone(recursive: true)
- Add the entity to the scene by creating an
AnchorEntity
and adding it as a child of the anchor.
let boxAnchor = AnchorEntity()
arView.scene.addAnchor(boxAnchor)
boxAnchor.addChild(boxCopy)
The clone will have the same transform as the original cube, so you'll need to modify each copy's transform to place them at a different location. See also https://developer.apple.com/documentation/realitykit/manipulating-reality-composer-scenes-from-code.