首页 > 解决方案 > I want to spawn my zombies in my game at a random location but I want to make sure to not spawn the inside the buildings

问题描述

Currently in my script I am spawning in 125 zombies randomly all over my map however with my current code it just spawns them everywhere including inside the buildings. I want to keep the random spawns so i can't make exact spawn locations. Does anyone know how to block them from spawning inside buildings?

My current code for spawning them in (I am a noob so sorry if code is bad/messy)

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

public class ZombieRandomSpawn : MonoBehaviour
{
    public GameObject ZK;
    public int xPos;
    public int zPos;
    public int enemyCount;


    // Start is called before the first frame update
    void Start()
    {
        while (enemyCount < 125)
        {
            xPos = Random.Range(1, 165);
            zPos = Random.Range(1, 215);
            Instantiate(ZK, new Vector3(xPos, 0.3f, zPos), Quaternion.identity);
            enemyCount = enemyCount + 1;
        }
    }

    
}

标签: c#visual-studiounity3d

解决方案


我会制作一个空游戏对象数组,这些对象可能是生成位置。然后将它们放置在建筑物外的任何您想要的地方。然后每次生成僵尸时,让它从您创建的位置数组中随机选择一个生成位置。


推荐阅读