c# - 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;
}
}
}
解决方案
我会制作一个空游戏对象数组,这些对象可能是生成位置。然后将它们放置在建筑物外的任何您想要的地方。然后每次生成僵尸时,让它从您创建的位置数组中随机选择一个生成位置。
推荐阅读
- javascript - 调用javascript方法变量丢失'this'
- machine-learning - 在 Ubuntu 16.04 上卸载 Caffe
- angular - Angular - 将额外的参数传递给带有路由的下一个组件
- machine-learning - 利用强化学习中学到的策略的方法
- python - 我如何组织 SQLAlchemy 关系以便像 JSON 一样的 dict 访问?
- ios - 尝试制作一个相机应用程序,对 Swift 来说是新的
- solr - SolrJ addFilterQuery 被忽略
- python-3.x - 以列表格式获取 KMeans 之后的聚类点
- sql - 如何通过 sql 数据库中的自定义 id 获取特定产品和组的最大计数
- angular - 设置可通过 ActivatedRoute 检索的特定路由参数