1
\$\begingroup\$

So I am trying to make random map generation for a game, and right now I am trying to use a raycast to get the height of the spot where a prefab will be generated, I am using a terrain and generating the mesh overtop, the mesh has a mesh collider on it. Right now, the only thing that happens is the raycast hits the terrain itself, how can I make it hit the mesh instead. here is my random position code:

    public void InstantiateRandomPosition(int Amount, float AddedHeight)
{
    var i = 0;
    float terrainHeight = 0f;
    RayCastHit hit;
    float randomPosX, randomPosY, randomPosZ;
    Vector3 randomPos = Vector3.zero;

    do
    {
        i++;
        randomPosX = Random.Range(TerrainLeft, TerrainRight);
        randomPosZ = Random.Range(TerrainBottom, TerrainTop);

        if (Physics.Raycast(new Vector3(randomPosX, 99999f, randomPosZ), Vector3.down, out hit, Mathf.Infinity, TerrainLayer))
        {
            terrainHeight = hit.point.y;
        }

        randomPosY = terrainHeight + AddedHeight;

        randomPos = new Vector3(randomPosX, randomPosY, randomPosZ);

        Instantiate(Test, randomPos, Quaternion.identity);

    } while (i < Amount);

}

can somebody help?

\$\endgroup\$
0

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.