【发布时间】:2018-06-08 05:35:12
【问题描述】:
我正在尝试创建像z-type 这样的坠落文字游戏。游戏开始后,屏幕上会显示几个单词。当用户键入一个字母时,如果该字母与显示的任何单词的第一个字母匹配,则会在该单词中添加一个activeWord 标签。我还创建了一个激光脚本,用于检查标签是否处于活动状态,当发生这种情况时,它会发射激光。
现在发生的情况是激光只发射一次,即当第一个字母匹配时,但在键入其余单词时不发射激光。
这是激光脚本:
using UnityEngine;
using UnityEngine.UI;
public class Laser : MonoBehaviour {
public float speed = 10.0f;
private Vector3 laserTarget;
private void Update () {
GameObject activeWord = GameObject.FindGameObjectWithTag("activeWord");
if (activeWord && activeWord.GetComponent<Text>().text.Length > 0) {
laserTarget = activeWord.transform.position; // find position of word
transform.Translate(laserTarget * speed * Time.deltaTime); // shoot the laser
}
}
}
我还添加了我在显示/UI 字段中使用的代码。
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 the word once all the letters match
Destroy(gameObject);
}
有人可以看看代码并告诉我哪里出错了。
【问题讨论】: