简体   繁体   English

Unity C# - 在一个点周围随机生成GameObject

[英]Unity C# - Spawning GameObjects randomly around a point

I am not sure how to approach this problem or whether there are any built in Unity functions that can help with this problem so any advice is appreciated. 我不知道如何处理这个问题或者是否有任何内置的Unity函数可以帮助解决这个问题所以任何建议都值得赞赏。

Here is an image that'll help describe what I want to do: 这是一张图片,有助于描述我想要做的事情: 在此输入图像描述

I want to spawn Game Objects around a given point within the limits of a set radius. 我想在设定半径范围内围绕给定点生成游戏对象。 However their position in this radius should be randomly selected. 但是,它们在此半径中的位置应随机选择。 This position should have the same Y axis as the origin point (which is on the ground). 该位置应与原点(在地面上)具有相同的Y轴。 The next main problem is that each object should not clash and overlap another game object and should not enter their personal space (the orange circle). 下一个主要问题是每个对象不应该与另一个游戏对象发生冲突和重叠,也不应该进入他们的个人空间(橙色圆圈)。

My code so far isn't great: 到目前为止我的代码不是很好:

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)));
    }
}

Thank you for any advice you can offer! 感谢您提供任何建议!

For spawning the object within the circle, you could define the radius of your spawn circle and just add random numbers between -radius and radius to the position of the spawner like this: 为了在圆圈内生成对象,您可以定义生成圆的半径,只需将-radius和radius之间的随机数添加到spawner的位置,如下所示:

float radius = 5f;
originPoint = spawner.gameObject.transform.position;
originPoint.x += Random.Range(-radius, radius);
originPoint.z += Random.Range(-radius, radius);

For detecting if the spawn point is to close to another game object, how about checking the distance between them like so: 为了检测生成点是否要靠近另一个游戏对象,如何检查它们之间的距离如下:

if(Vector3.Distance(originPoint, otherGameObject.transform.position < personalSpaceRadius)
{
    // pick new origin Point
}

I'm not that skilled in unity3d, so sry for maybe not the best answer^^ 我对unity3d不熟练,所以可能不是最好的答案^^

Also : 另外

To check which gameobjects are in the spawn area in the first place, you could use the Physics.OverlapSphere Function defined here: http://docs.unity3d.com/ScriptReference/Physics.OverlapSphere.html 要首先检查生成区域中的哪些游戏对象,可以使用此处定义的Physics.OverlapSphere函数: http//docs.unity3d.com/ScriptReference/Physics.OverlapSphere.html

For personal space you can use colliders to avoid overlapping. 对于个人空间,您可以使用colliders避免重叠。

For spawning in circle you can use Random.insideUnitSphere . 对于在circle中生成,可以使用Random.insideUnitSphere You can modify your method as, 你可以修改你的方法,

 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
        Vector3 point = (Random.insideUnitSphere * spawnRadius) + originPoint;
        Instantiate (agent, point, Quaternion.Euler (new Vector3 (0f, directionFacing, 0f)));
    }

Hope this helps you. 希望这对你有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM