Unity 如何限制Spawner?

萨姆拉特·卢特尔

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


}

感谢您的时间和考虑

穆克什·赛尼

尝试使用静态变量 -

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章