Place objects on the ground
Updated
Two trees, a rock and a signpost saved floating above a hilly terrain drop onto its surface at startup: a Terrain component read with heightAt by a Behavior script, with no raycast.
Orbit around: every object rests on the slope under it. Drag to orbit the camera, scroll to zoom.
Place objects on the ground
#pragma lifecycle(startup)
const PROPS = ['Tree 1', 'Tree 2', 'Rock', 'Signpost'];const point = new THREE.Vector3();
function startup() { const ground = findEntity('Terrain'); const terrain = ground.getComponent(TerrainComponent);
for (const name of PROPS) { const prop = findEntity(name); if (!prop) continue;
prop.getWorldPosition(point); ground.worldToLocal(point); point.y = terrain.heightAt(point.x, point.z); ground.localToWorld(point);
prop.setPosition(point.x, point.y, point.z); }}How it works
Section titled “How it works”The script is explained step by step in Place objects on the ground.
Open it in XR Creator
Section titled “Open it in XR Creator”Download the project file below, then import it in XR Hub with Projects › New › Import, as described in Projects and templates. It opens in XR Creator with the same scene you see running above, each script already attached to its Behavior entity. From there you can edit the scripts, adjust components and their props, preview your changes, and deploy the result as a world of your own.
SnapToTerrain.xrproj