我想让一些敌人在4个不同的地方产生生存型游戏。问题是,它们都在同一个地方产卵。为什么是这样?顺便说一句,这就是在Unity中。
C#脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour {
public int spawnHere = 0;
public int spawnTimer = 0;
public int spawnRate = 1;
public Transform spawner1;
public Transform spawner2;
public Transform spawner3;
public Transform spawner4;
public GameObject melee1;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
spawnTimer = spawnTimer + spawnRate;
spawnHere = Random.Range (1, 5);
if(spawnTimer >= 120) {
if(spawnHere == 1) {
Instantiate (melee1, spawner1);
}
if(spawnHere == 2) {
Instantiate (melee1, spawner2);
}
if(spawnHere == 3) {
Instantiate (melee1, spawner3);
}
if(spawnHere == 4) {
Instantiate (melee1, spawner3);
}
spawnTimer = 0;
}
}
}
答案 0 :(得分:0)
您是否已在UI中正确连接了产品?
请注意:Unity - Survival Shooter - Spawning Enemies
这对我的项目非常有用。
您还应该使用Time.deltaTime来计算生成时间。并非每个系统都输出相同数量的帧/秒。
顺便说一句:
Random.rand(min,max)
Unity - docs - Random.Rand 包括max作为可能的值。