- Home /
Question by
Edelweiss7 · Jun 11, 2016 at 07:14 PM ·
overlapsphere
My OverlapSphere and Layer Mask isn't working.
So what I'm trying to do is instantiate a bunch of items at random points, each a certain distance apart. Reading around, I saw that using Layermasks and the Physics OverlapSphere would be my best bet. But I can't figure out what's wrong with my code for the life of me. No matter what I do, all the items spawn at the exact same spot.
public GameObject pickup;
private GameObject gameBike;
private Vector3 gameBikePosition;
private int numberOfPickups = 10;
private float distributionRadius = 25;
private float distanceBetweenPickups = 10;
private float pickupHeight = 1;
private bool positionIsValid = false;
public LayerMask itemLayer;
private Vector3 pickupPosition;
Vector3 CheckForNeighbours(){
int mask = LayerMask.NameToLayer("Pickup");
while (positionIsValid == false)
{
float xPosition = Random.Range(gameBikePosition.x - distributionRadius, gameBikePosition.x + distributionRadius);
float zPosition = Random.Range(gameBikePosition.z - distributionRadius, gameBikePosition.z + distributionRadius);
pickupPosition = new Vector3(xPosition, 0, zPosition);
float height = Terrain.activeTerrain.SampleHeight(pickupPosition) + pickupHeight;
pickupPosition.y = height;
Collider[] neighbours = Physics.OverlapSphere(pickupPosition, distributionRadius, 1<<9);
if (neighbours.Length == 0){
positionIsValid = true;
return pickupPosition;
}else{
positionIsValid = false;
}
}
return pickupPosition;
}
Comment