【问题标题】:Unity How to limit Spawner?Unity如何限制Spawner?
【发布时间】:2017-05-02 17:37:36
【问题描述】:

我正在开发一款类似 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);
    }


}

感谢您的时间和考虑

【问题讨论】:

  • 你能检查一个正方形是否已经在某个时间范围内产生了吗?或者如果屏幕上有一个正方形?基本上引入了一个条件,在该条件下可以生成圆形或正方形,而不是完全随机
  • @Luke K 我想过类似的事情,但无法以编程方式编写

标签: c# unity3d


【解决方案1】:

试试这个,

  1. 在最小-最大周期之间一次只会生成一个对象
  2. 它不会清理旧对象
  3. 它允许超过 2 个预制件

我尽量保持你的代码格式

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

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

【讨论】:

    【解决方案2】:

    尝试使用静态变量 -

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

    【讨论】:

    • 非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-13
    • 2015-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多