- Home /
Spawn a mesh in terrain
Hi! So what I want is to spawn a mesh in a random location in my terrain. But sometimes it spawns inside it and ejects in the skies like crazy! So I thought that if there was a way to make it spawn a little higher than the surface of the terrain whould solve the problem. So I'm stuck! I know that raycast could be useful, but I don't understand how they work... So please help me!!! :)
Answer by robertbu · Oct 28, 2014 at 06:07 PM
For a Unity Terrain, you can use Terrain.SampleHeight. Take a look at the sample script:
http://docs.unity3d.com/ScriptReference/Terrain.SampleHeight.html
Note that if the pivot of your object is not at the bottom of your object, you will need to add a constant amount to the 'y' to put your object on top of the terrain.
Thank you so much! It works even better than I thought!!!!
Your answer
![](https://koobas.hobune.stream/wayback/20220613170154im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to randomly spawn objects above the ground? 1 Answer
Making a particle effect parallel to the slope of a terrain 1 Answer
How to spawn 2 events with a score gap..? 0 Answers
player spawning 0 Answers
Script causing lag 1 Answer