I have mountains, valleys and hills. But objects only spawn near valleys and in lower areas, not on hills:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ResourceGenerator : MonoBehaviour
{
[Header("Spawn settings")]
public GameObject resourcePrefab;
public float spawnChance;
[Header("Raycast setup")]
public float distanceBetweenCheck;
public float heightOfCheck = 1000f, rangeOfCheck = 30f;
public LayerMask layerMask;
public Vector2 positivePosition, negativePosition;
private void Start()
{
SpawnResources();
}
void SpawnResources()
{
for(float x = negativePosition.x; x < positivePosition.x; x += distanceBetweenCheck)
{
for(float z = negativePosition.y; z < positivePosition.y; z += distanceBetweenCheck)
{
RaycastHit hit;
if(Physics.Raycast(new Vector3(x, heightOfCheck, z), Vector3.down, out hit, rangeOfCheck, layerMask))
{
if(spawnChance > Random.Range(0f, 101f))
{
Instantiate(resourcePrefab, hit.point, Quaternion.Euler(new Vector3(0, Random.Range(0, 360), 0)), transform);
}
}
}
}
}
}
Well
your raycast is checking from a certain
heightOfCheckdownwards whether it hits your terrain / ground.Further it is limited to only check at a maximum distance of
rangeOfCheckfrom thatheightOfCheck.=> For
rangeOfCheckyou should make sure it can even reach far enough down to hit your surface ->rangeOfCheckshould be at least>= heightOfCheckdepending whether your terrain can also have ground levels< 0.So for instance it can only hit the ground at places where
ground level < heightOfCheckand at the same time where therangeOfCheckis big enough to reach down to the ground.So assuming your terrain has only positive ground levels with a maximum height of
200I would use something slightly bigger like e.g.heightOfCheck = 205and then ensure thatrangeOfCheckcan actually again reach0so I would again add a little buffer like e.g.rangeOfCheck = heightOfCheck + 5;.The
5is completely arbitrary - could be any positive value that ensures you start high enough and also end low enough, so you could use a single variable field and do e.g.