I’m working on a project in Niantic Studio and trying to create a simple AR game. The goal is to randomly spawn grass and tech objects in the AR environment at different intervals. The player should:
- Touch grass to score points.
- Avoid tech to prevent losing points.
So far, I’ve:
- Imported
grass.glb
and laptop.glb
models into my project.
- Configured their colliders in the editor (grass is event-only, tech has dynamic physics).
- Set up basic scripts for handling collisions and scoring.
The problem I’m running into is spawning objects randomly in the AR world. Specifically:
- How can I script random spawning of grass and tech at different positions within a defined range?
- Is there a way to handle the spawning intervals directly in the Studio, or does it need to be fully scripted?
I’d really appreciate examples, documentation pointers, or advice from anyone who has tackled random spawning in Niantic Studio before.
#nianticstudio #spawning #ar-development #colliders #gameplay
Welcome to the forums!
The way I’d recommend is to create your random entities in a radius around the origin: 0, 0, 0 which maintaining a 0 y-coordinate since SLAM tracking will ensure that the 0 y-coordinate corresponds to the real world ground.
There isn’t a way to do this without code, however creating a custom component shouldn’t be too difficult. Here’s a starting point:
import * as ecs from '@8thwall/ecs'
ecs.registerComponent({
name: 'random-grass',
schema: {
},
schemaDefaults: {
},
data: {
},
add: (world, component) => {
world.time.setInterval(() => {
const grass = world.createEntity()
const x = (Math.random() - 0.5) * 20 // Random x position between -10 and 10
const z = (Math.random() - 0.5) * 20 // Random z position between -10 and 10
const y = 0 // Always set y to 0
world.setPosition(grass, x, y, z)
ecs.GltfModel.set(world, grass, {
url: 'assets/grass.glb',
})
console.log('created a grass patch')
}, 5000) // Spawn every 5 seconds.
},
tick: (world, component) => {
},
remove: (world, component) => {
},
})
Note: Make sure your grass.glb is named grass.glb
, it’s not too big, and it’s at the correct origin like this:
thanks! Let me test this out and get back to you!