Unity如何限制Spawner?

时间:2017-05-02 08:53:42

标签: c# unity3d

我正在制作像2辆汽车这样的游戏。所以会有两条线,我使用了两个对象产生器,它将产生两个形状,即圆形和方形。所以当玩家与圈子碰撞时应该更新。当Square落入时,玩家应该通过前往另一条车道来避开它。但是问题是产生者同时产生正方形还是间隙小的问题。所以玩家无法逃脱。任何解决方案。嗯,我想这没什么用,但这是我的剧本

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

public class Instantiter : MonoBehaviour {
    public GameObject[] gameobject;
    public float  SpawnDelay= 3f;
    private GameObject objectkeeper;
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        float Spawntime = SpawnDelay * Time.deltaTime; // 1 *1/60
        if (Random.value < Spawntime) {
            Spawn ();
        }
    }

    void Spawn(){
        int number = Random.Range (0, 2);// creating random number between 0 and 1
        objectkeeper = Instantiate (gameobject [number], this.transform.position, Quaternion.identity) as GameObject;
        objectkeeper.transform.parent = this.transform;

    }

    void OnDrawGizmos(){
        Gizmos.DrawWireSphere (this.transform.position, 0.5f);
    }


}

感谢您的时间和考虑

2 个答案:

答案 0 :(得分:1)

试试这个,

  1. 它只会在最小 - 最大周期
  2. 之间一次产生一个对象
  3. 它不会清理旧对象
  4. 它允许超过2个预制件
  5. 我尽可能地保持你的代码格式

    免责声明:我目前没有开放视觉工作室/单声道开放(在无聊的会议中)所以我没有测试过这个:]

    Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().IsScreenCaptureEnabled = false;
    

答案 1 :(得分:0)

尝试使用静态变量 -

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

public class Instantiter : MonoBehaviour {
    public GameObject[] gameobject;
    public float  SpawnDelay= 3f;
    private GameObject objectkeeper;

    // Static variable shared across all instances of this script
    public static float nextSpawn = 0f;
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        // check if SpawnDelay duration has passed
        if (Time.time >= nextSpawn) {
            // Now spawn after ramdom time
            float Spawntime = SpawnDelay * Time.deltaTime; // 1 *1/60
            if (Random.value < Spawntime) {
                Spawn ();
            }
        }
    }

    void Spawn(){
        int number = Random.Range (0, 2);// creating random number between 0 and 1
        objectkeeper = Instantiate (gameobject [number], this.transform.position, Quaternion.identity) as GameObject;
        objectkeeper.transform.parent = this.transform;
        // Set the nextSpawn time to after SpawnDelay Duration
        nextSpawn = Time.time + SpawnDelay;
    }

    void OnDrawGizmos(){
        Gizmos.DrawWireSphere (this.transform.position, 0.5f);
    }
}