【问题标题】:Enemies All Spawning In Same Place敌人都在同一个地方产卵
【发布时间】:2017-09-10 12:50:08
【问题描述】:

我希望一些敌人在 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;

  }
 }
}

【问题讨论】:

  • 可能不是你的问题,但最后一种情况应该导致Instantiate (melee1, spawner4); 不是吗?不是Instantiate (melee1, spawner3);
  • 哦,是的,但正如你所说,这不是问题。
  • 你打印了spawnHere 的值来看看它给出了什么?
  • 是的。他们甚至是他们的产卵者的孩子。

标签: c# unity3d


【解决方案1】:

您是否在 UI 中正确连接了生成器?

观看此视频:Unity - Survival Shooter - Spawning Enemies

这对我的项目非常有效。

您还应该使用 Time.deltaTime 来计时生成。并非每个系统每秒输出相同数量的帧。

Unity - docs - Time.deltaTime

顺便说一句:

Random.rand(min, max)

Unity - docs - Random.Rand 包括 max 作为可能的值。

【讨论】:

  • btw UnityEngine.Random.Range (1, 5); 不包括 5 作为可能的值。
猜你喜欢
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 2022-11-01
  • 1970-01-01
  • 2016-10-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多