我不确定如何解决这个问题,或者是否有任何内置的Unity函数可以帮助解决这个问题,所以非常感谢您提供任何建议。
我想在给定点周围的半径范围内生成游戏对象。然而,它们在半径范围内的位置应该是随机选择的。这个位置应该与原点(在地面上)具有相同的Y轴。下一个主要问题是每个物体都不应该与其他游戏对象重叠,并且不应该进入它们的个人空间(橙色圆圈)。
到目前为止,我的代码还不太好:
public class Spawner : MonoBehaviour {
public int spawnRadius = 30; // not sure how large this is yet..
public int agentRadius = 5; // agent's personal space
public GameObject agent; // added in Unity GUI
Vector3 originPoint;
void CreateGroup() {
GameObject spawner = GetRandomSpawnPoint ();
originPoint = spawner.gameObject.transform.position;
for (int i = 0; i < groupSize; i++) {
CreateAgent ();
}
}
public void CreateAgent() {
float directionFacing = Random.Range (0f, 360f);
// need to pick a random position around originPoint but inside spawnRadius
// must not be too close to another agent inside spawnRadius
Instantiate (agent, originPoint, Quaternion.Euler (new Vector3 (0f, directionFacing, 0f)));
}
}
感谢您能提供的任何建议!