Home > Enterprise >  spawning object on the ground in terrain
spawning object on the ground in terrain

Time:12-07

I created a randomly spawn animal script but my animals spawning inside the hill and my terrain has a lot of hill. How do i make the animals spawn always on the ground.

Note: Normally my terrains y axis value is 0.

Here is my code.

IEnumerator spawnenemiesatposition1()
{
yield return new WaitForSeconds(3f);
while (cond1)
{
    Vector3 position1 = new Vector3(Random.Range(5, 195), 1, Random.Range(5, 495));
    Instantiate(Deer, position1, Quaternion.Euler(-90, 0, 0));
    deernum1  ;
    if (deernum1 > 5)
    {
        cond1 = false;
    }

}

Example of my issue

How could i make the animals spawn always on the ground.

CodePudding user response:

Use Terrain.SampleHeight.

You can find the documentation here.

Basically, given your world position, it returns you the height of the terrain in that position.

  • Related