简体   繁体   English

如何在给定区域随机生成对象?

[英]How can i spawn Objects randomly in a given area?

So im building a small Bulletgell game, and im at the point where I want Enemys to spawn in at random Spawnpoints in a given area.因此,我正在构建一个小型 Bulletgell 游戏,并且我希望敌人在给定区域的随机 Spawnpoints 中生成。 I did that with help of the BoxCollider2D, everything works fine but I want them to spawn OUTSIDE the actuall Map so they need to walk in the Map and Screen where the Player is.我在 BoxCollider2D 的帮助下做到了这一点,一切正常,但我希望它们在实际 Map 之外生成,因此他们需要走进 Map 和播放器所在的屏幕。 I just can spawn them in the whole are of the BoxCollider.我可以在整个 BoxCollider 中生成它们。 Is there a way to determine which area the SpawnScript can use?有没有办法确定 SpawnScript 可以使用哪个区域?

In the Picture the red marked area should be the area where the Enemy spawn randomly.在图片中,红色标记的区域应该是敌人随机生成的区域。

Thanks for the help:)谢谢您的帮助:) 在此处输入图像描述

Heres the code im using atm:这是我使用atm的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class box : MonoBehaviour
{
public GameObject spawnedPrefab;
public BoxCollider2D spawnArea;
Vector2 maxSpawnPos;

float lastSpawnTimeS = -1;
public float spawnDelayS = 5;

// Use this for initialization
void Start()
{
    spawnArea = GetComponent<BoxCollider2D>();
    spawnArea.enabled = false; 
    maxSpawnPos = new Vector2(spawnArea.size.x / 2, spawnArea.size.y / 2);
}

// Update is called once per frame
void Update()
{
    if (lastSpawnTimeS < 0)
    {
        lastSpawnTimeS = Time.time;
        GameObject spawned = Instantiate(spawnedPrefab, Vector3.zero, Quaternion.identity) as GameObject;
        spawned.transform.parent = transform;
        Vector3 pos = new Vector3(Random.Range(-maxSpawnPos.x, maxSpawnPos.x), Random.Range(-maxSpawnPos.y, maxSpawnPos.y), 0);
        spawned.transform.localPosition = pos;
    }
    else if (lastSpawnTimeS >= 0 && Time.time - lastSpawnTimeS > spawnDelayS)
    {
        lastSpawnTimeS = -1;
    }
}

} }

If object tries to go outside spawn area it will reset it to zero.如果 object 尝试 go 超出生成区域,它将重置为零。 You can also place if condition if object placed at zero add some random position within area.如果 object 置于零,您也可以在区域内添加一些随机 position。

if (lastSpawnTimeS < 0)
{
    lastSpawnTimeS = Time.time;
    GameObject spawned = Instantiate(spawnedPrefab, Vector3.zero, Quaternion.identity) as GameObject;
    spawned.transform.parent = transform;
    Vector3 pos = new Vector3(Random.Range(-maxSpawnPos.x, maxSpawnPos.x) % spawnArea.size.x, Random.Range(-maxSpawnPos.y, maxSpawnPos.y) % spawnArea.size.y, 0);
    spawned.transform.localPosition = pos;
}
else if (lastSpawnTimeS >= 0 && Time.time - lastSpawnTimeS > spawnDelayS)
{
    lastSpawnTimeS = -1;
}

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

相关问题 如何在给定的体积/区域 3d 中生成对象 - How can I spawn objects in a given volume/area 3d 如何生成在特定给定区域之间不重叠的随机对象? - How can i spawn random objects that are not overlap between specific given area? 如何在平面尺寸中随机生成对象? - How to randomly spawn objects in plane dimensions? 在已经存在的对象数量为零之后,如何随机生成 object? - How do I randomly spawn object after the number of those objects already existing is zero? 如何设置要生成的游戏对象数量? - how can I set an amount of game objects to spawn? 如何从他当前的 position 在小区域内随机移动 object? - How can I move an object randomly in small area from his current position? 如何找到平面大小和 position 并在其上生成随机对象? - How can I find a Plane size and position and spawn random objects over it? 如何在Unity 2D中在两个指定的位置随机生成两个不同的对象? - How to Spawn Two Different Objects Randomly in Two Specified Positions in Unity 2D? 如何在 Unity 中生成对象? - How to spawn objects in Unity? 我想在给定区域的随机 position 中生成敌人。 我不想在障碍物顶部等任何地方随机生成我的敌人 position - I want to spawn Enemy's in random position in given area. I don't want to spawn my enemy's in random position anywhere like top of obstacle and such
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM