0
\$\begingroup\$

I'm trying to create a falling word game like z-type (code here). Once the game starts a few words are displayed on the screen. When the user types a letter and if that matches with the first letter of any of the words displayed, an "activeWord" tag is added to the word. I have also created a laser script that checks if the tag is active and when that happens, it shoots the laser. What's happening right now is that the laser is shot only once i.e when the first letter matches but doesn't shoot a laser when the remaining words are typed. Can someone please have a look at the code and tell me where I'm making a mistake.

this is the word display script where the active word is assigned:

public void RemoveLetter() // remove the first letter if its correct and so on for the remaining letters. change the color of the word to red and add the "activeddWord" tag.
 {
 text.text = text.text.Remove(0, 1);    
 text.color = Color.red;
 text.gameObject.tag = "activeWord";        
 }

public void RemoveWord()
{
    Destroy(gameObject); // removes the word from display         
}

here is the input manager script:

public class WordInput : MonoBehaviour {

public WordManager wordManager;


void Update () {
    foreach (char letter in Input.inputString)
    {
        wordManager.TypeLetter(letter);
    }
}

EDIT

This is the player script where the laser is instantiated:

public class Player : MonoBehaviour {
public GameObject laserPrefab;
void Start () {
    transform.position = new Vector3(0.0f, -3.92f, 0); // player position                       
}

void Update () {       
      Instantiate(laserPrefab, transform.position, Quaternion.identity);
        }
}

This is the updated laser script:

public class Laser : MonoBehaviour
{
public float speed = 10.0f;

private Vector3 laserTarget;

private void Start()
{

}

GameObject activeWord = GameObject.FindGameObjectWithTag("activeWord");

if (activeWord != null && activeWord.GetComponent<Text>().text.Length > 0){

  laserTarget = activeWord.transform.position; // find position of word
  transform.Translate(laserTarget * speed * Time.deltaTime);

  for (int i = 0; i < Input.inputString.Length; i++){

   Debug.Log("Character from input string: " + Input.inputString[i] + "\n" +
   "Character from word displayed: " + activeWord.GetComponent<Text>().text[0]);

   if (Input.inputString[i] == activeWord.GetComponent<Text>().text[0]){

     laserTarget = activeWord.transform.position; // find position of word
     transform.Translate(laserTarget * speed * Time.deltaTime); // shoot the laser
  }
 }
}

After I do this, there are two major errors that I get:

  1. As soon as I hit play, the laser fires continuously without stopping.
  2. The laser still shoots only once after the word is typed...This is the message I get in the console (screenshot attached). There is a difference of one word between the input string and whats displayed on the screen...Can someone please tell me how to rectify these.....

enter image description here

\$\endgroup\$
1
\$\begingroup\$

Firstly, every time you remove a letter, you assign it the tag "activeWord". So when you find with tag, it just finds the first object assigned with this tag and targets it. Maybe this is why your laser is shot once, because the laser is shot and it just goes and sticks at activeword.transform.position.

Secondly i do not see any line of code that actually destroys the word after it has been hit. That way your laser will target the next game object with tag "activeWord" automatically, and you wont need to write extra code.

If you want multiple lasers to be hitting at once, it is a good idea create an empty object that never moves in level and set it as parent to your laser shots. When a word is typed, take its position, assign it to a laser shot. parent it to empty and change tag for that word. Once that word is hit, destroy it. That way each laser will have a different position to go to.

Good Luck.

\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.