spawning object on the ground in terrain

spawning object on the ground in terrain

Problem Description:

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.

Solution – 1

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.

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject