简体   繁体   中英

Unity How to limit Spawner?

I am working on a game like 2 cars. So there will be two lines and I have used two object spawner which is going to spawn two shapes, i,e Circle and Square. So when player collides with circle score should update. And when Square falls player is supposed to avoid it by going to another lane. But what is the problem is something both the spawner spawns square simultaneously or with small gap. So player is not able to escape. Any solution for this. Well I guess it does not help much but here is my script

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);
    }


}

Thank you for your time and consideration

Try this,

  1. It will only spawn one object at a time between the min-max period
  2. It does not cleanup old objects
  3. It allows for more than 2 prefabs

I tried keeping to your code format as much as possible

Disclaimer : I do not currently have a visual studio/mono develop open (in a boring meeting) so i have not tested this :]

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

public class Instantiter : MonoBehaviour {
    public GameObject[] prefabs;
    // Adding a min-max allows full control of spawning behaviour without editing code again.
    // for a fixed time use the same value
    public float  MinimumSpawnDelay = 3f;
    public float  MaximumSpawnDelay = 6f;

    private GameObject spawnedObject;
    // Made this static so it retains it's value across all instances of this script. 
    // If you want each Instantiter object to function on it's own, remove the static keyword
    private static float nextSpawnTime;

    // Use this for initialization
    void Start () {
        // Artificial delay so we do not spawn an object directly at startup
        SetNextSpawnTime();
    }

    // Update is called once per frame
    void Update () {
        if (Time.time >= nextSpawnTime) {
            Spawn ();
        }
    }

    void Spawn(){
        // allows you to add more objects to the prefabs array without changing code.
        var prefabToSpawn = prefabs[Ranom.Range(0, prefabs.Length)];

        spawnedObject = Instantiate (prefabToSpawn, transform.position, Quaternion.identity);
        spawnedObject.transform.parent = transform;

        SetNextSpawnTime();
    }

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

    void SetNextSpawnTime(){
        // a simple variable to hold when we should spawn another object, efficient.
        nextSpawnTime = Time.time + Random.Range(MinimumSpawnDelay, MaximumSpawnDelay);
    }
}

Try using a static variable -

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM