When initially adding a GameObject prefab to a scene, is there some way to achieve the following outcome without all the manual labor I'm presently doing in between?

  • Add GameObject to Scene so it's positioned slightly above the non-flat terrain's surface
  • Ensure that it has a Rigidbody and Collider, and both are properly configured.
  • Hit 'play' so the GameObject falls straight down & lands on the terrain's surface.
  • Select the GameObject, then copy its transform
  • Stop 'play', then paste the copied transform's values into the same GameObject (so it positions it in the exact spot gravity, its rigidbody, and its collider would have it end up moments after play begins).

Likewise, when instantiating a GameObject via C#, is there some way to tell Unity, "position it at (x,z), at whatever 'y' value will place the lowest point of its lowest collider precisely on the surface of the terrain at (x,z)"?

Finally, is there a setting somewhere in Unity to make colliders+GameObjects "buoyant" relative to terrain surface, so that if the GameObject does somehow end up with a transform that positions it below the terrain's surface, Unity will just instantly and unceremoniously "float" it back up instead of having it fall through the ground and plummet into a virtual black hole?

  • \$\begingroup\$ How complex are the GameObjects we are talking about? And you are using prefabs for your second points, right? You do not need to adjust the rb and collider for each one that way \$\endgroup\$
    – Zibelas
    Jan 5 at 8:37
  • \$\begingroup\$ About this complex: assetstore.unity.com/packages/3d/characters/animals/…. Yes, I'm using prefabs for the second point. I wouldn't really be adjusting the rb and collider, I just mentioned them to be complete and emphasize that they're correctly set. The main thing is that I can't seem to find the magic way to place them in the scene so they're exactly at ground level, vs slightly above and having to fall a few pixels down onto it, or slightly below and falling into the virtual black hole below the terrain. \$\endgroup\$
    – Bitbang3r
    Jan 5 at 8:52
  • \$\begingroup\$ Sounds like you just want a basic raycast to check the height on spawn. We have past Q&A about using raycasts to find terrain intersections. \$\endgroup\$
    – DMGregory
    Jan 5 at 12:39

You must log in to answer this question.

Browse other questions tagged .