Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 统一如何限制产卵者?_C#_Unity3d - Fatal编程技术网

C# 统一如何限制产卵者?

C# 统一如何限制产卵者?,c#,unity3d,C#,Unity3d,我正在做一个像两辆车一样的游戏。所以将有两条线,我使用了两个对象产卵器,它将产卵两个形状,即圆形和方形。所以当玩家与圆圈碰撞时,分数应该更新。当广场摔倒时,玩家应该去另一条车道躲避。但是问题是产卵者同时产卵或者产卵间隔很小。所以玩家无法逃脱。任何解决办法。嗯,我想这没什么帮助,但这是我的剧本 using System.Collections; using System.Collections.Generic; using UnityEngine; public class Instantite

我正在做一个像两辆车一样的游戏。所以将有两条线,我使用了两个对象产卵器,它将产卵两个形状,即圆形和方形。所以当玩家与圆圈碰撞时,分数应该更新。当广场摔倒时,玩家应该去另一条车道躲避。但是问题是产卵者同时产卵或者产卵间隔很小。所以玩家无法逃脱。任何解决办法。嗯,我想这没什么帮助,但这是我的剧本

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个以上的预制件 我尽量保持你的代码格式

免责声明:我目前没有在无聊的会议上打开visual studio/mono develop,因此我没有对此进行测试:]

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

尝试使用静态变量-

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

你能检查一个正方形是否已经在某个时间范围内生成了吗?或者如果屏幕上有一个正方形?基本上引入一个条件,在这个条件下,一个圆或一个正方形都可以生成,而不是完全生成random@LukeK我也想到过类似的事情,但不能用程序来写。我非常感谢你