简体   繁体   中英

Wave spawner and wave number for typing game

I'm trying to create a word typing game where words fall from the top of the screen. I have used the code provided here to get started:

In the WordSpawner.cs file, I want the words to spawn in waves like the first wave would have 2 words and once those have been typed correctly, the next wave will start after a delay of about 5 seconds and will have 3 words and so on. The number of words should increase by 1 in every wave and will continue to do so while the player is alive.

Once one wave is completed, there will be a wave number displayed during the wait time and then the next wave starts.

This is the script for the word spawner:

public GameObject wordPrefab; 
public Transform wordCanvas; 

public WordDisplay SpawnWord () {

  Vector3 randomPosition = new Vector3(Random.Range(-2.5f, 2.5f), 7f); 
  GameObject wordObj=Instantiate(wordPrefab, randomPosition,Quaternion.identity, wordCanvas); 
  WordDisplay wordDisplay = wordObj.GetComponent<WordDisplay>(); 
  return wordDisplay;
  } 

I tried following the official unity space shooter for spawning enemies but I'm unable to get it to work. I think its because the SpawnWord() method above has a return type and then this function is called in a separate file "WordManager". This the script from the word manager file:

public class WordManager : MonoBehaviour {

public List<Word> words;// list of 50 words 

public WordSpawner wordSpawner;

private bool hasActiveWord;
private Word activeWord;

public void AddWord ()
{
    Word word = new Word(WordGenerator.GetRandomWord(), wordSpawner.SpawnWord()); // this is where the SpawnWord() is called
    words.Add(word);
}

Assuming your question is how to have the word spawner work in waves, I would do the following:

The problem can be broken into 3 sections: 1) Tracking when the player has entered all words needed. 2) Running a timer to delay the start of the next wave. 3) Spawning the words.

It looks like you already have the word spawning and removal going, so I'll focus on tracking the game states in part 2.

Now fortunately for us Unity provides us the Update function which will run every frame. The two things we'll need to do from here is check how many active words we have in our list, and we'll need a timer to track our delay between waves.

In order to accomplish this you'll want to have a bit of code running in thus Update function to check for if the list is empty, and to check for if a timer has expired (being the delay between waves). From there if you find the case where the wave is over and the timer is expired, call your functions to spawn the new wave of words.

And if you want to increase the number spawned each wave, keep a class integer that is incremented each time a wave runs and provide it as a parameter for how many words to spawn.

If I wasn't clear on anything let me know and I'll do my best to explain.

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