2017-05-02 5 views
0

Ich arbeite an einem Spiel wie 2 Autos. Also wird es zwei Linien geben und ich habe zwei Objekt-Spawner benutzt, die zwei Formen hervorbringen werden, nämlich Kreis und Quadrat. Wenn also der Spieler mit dem Kreis kollidiert, sollte die Punktzahl aktualisiert werden. Und wenn Square fällt, soll der Spieler es vermeiden, indem er auf eine andere Spur geht. Aber was ist das Problem ist etwas, das der Spawner gleichzeitig oder mit kleiner Lücke platzt. So kann der Spieler nicht entkommen. Irgendeine Lösung dafür. Nun, ich denke, es hilft nicht viel, aber hier ist mein SkriptUnity Wie Spawner zu begrenzen?

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


} 

Vielen Dank für Ihre Zeit und Überlegung

+0

können Sie überprüfen, ob ein Platz bereits innerhalb eines bestimmten Zeitraums hervorgebracht worden ist? Oder wenn ein Quadrat überhaupt auf dem Bildschirm ist? Stellen Sie im Grunde eine Bedingung, unter der entweder ein Kreis oder ein Quadrat spawnen kann, statt es völlig zufällig –

+0

@Luke K Ich habe an eine ähnliche Sache gedacht, aber nicht programmatisch schreiben –

Antwort

0

Versuchen Sie, eine statische 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); 
    } 
} 
+0

Vielen Dank –

+0

Ihre Begrüßung :) –

1

die Sie interessieren,

  1. Es wird nur laichen ein Objekt zu einer Zeit zwischen die min-max-Periode
  2. Es spielt keine Bereinigung alte Gegenstände
  3. Es ermöglicht mehr als 2 prefabs

Ich habe versucht, so viel wie möglich

Haftungsausschluss Code-Format zu halten: ich derzeit keine Visual Studio/Mono entwickeln geöffnet haben (in einer langweiligen Sitzung), so dass ich habe nicht getestet:]

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