Hi.
Well, I’m trying to make a “random enemy spawn” script, who will populate dynamically my scene based on the NavMesh generated by Unity (so the enemy will not spawn inside a building or an “off-limits” area).
I’ve found some examples in Unity Answers but haven’t got any good results yet. In the examples, they used a mix of Vector3 Random.Range vars and NavMesh.SamplePosition to get the approximated result (of the random value) in NavMesh, but here I get the same position every time (even randomizing between -1000 and 1000 or -100 and 100).
Anyone have made something like that before and can bring me a light?
* *int layerMask = (1 << NavMesh.GetAreaFromName("Default")); var position = new Vector3(Random.Range(-50f, 50f), 0, Random.Range(-50f, 50f)); NavMeshHit hit; NavMesh.SamplePosition(position, out hit, 50f, layerMask); Instantiate(ZombiePrefab, hit.position, new Quaternion(0, 0, 0, 0));* *
Thanks!
EDIT: The first problem was solved. I just missed the “NavMesh LayerMask” ID and the “distance float” is lower than the min/max X and Z positions, so the hit.position returns an Infinity Vector3.
Now I have another problem. Just see my post above.