我正在游戏中实现一个门户系统,但是有一点问题。我的播放器附有WorldInteraction脚本。每当玩家在门户上发生冲突时,玩家都需要按Enter键以选择位置。现在我的问题是为什么在按下Enter键后为什么控制台中的日志太多(出于测试目的)?例如,如果我在控制台中有3个日志,那么我的门户网站按钮也将是3。
这就是我在说的。请参见下面的屏幕截图: 如您所见,控制台中有3个日志,按钮也相同。如何修复该错误?每个地图应该有1个按钮。
门户:
画布:
门户:
WorldInteraction.cs
public class WorldInteraction : MonoBehaviour {
public GameObject messagePanel;
private bool hasInteracted = false;
void OnTriggerEnter(Collider interactedObject) {
messagePanel.SetActive (true);
if (interactedObject.gameObject.tag == "Interactable Object")
{
messagePanel.GetComponentInChildren<Text> ().text = "Press F to interact";
}
else if (interactedObject.gameObject.tag == "Pickupable Object")
{
messagePanel.GetComponentInChildren<Text>().text = "Press Left Control to pick up";
}
else if (interactedObject.gameObject.tag == "Portal")
{
messagePanel.GetComponentInChildren<Text> ().text = "Press Enter to select location";
}
}
void OnTriggerStay(Collider interactedObject) {
if (interactedObject.gameObject.tag == "Interactable Object" && Input.GetKeyDown (KeyCode.F)) {
hasInteracted = true;
GetInteraction (interactedObject);
}
else if (interactedObject.gameObject.tag == "Pickupable Object" && Input.GetKeyDown(KeyCode.LeftControl)) {
hasInteracted = true;
GetInteraction(interactedObject);
}
else if(interactedObject.gameObject.tag == "Portal" && Input.GetKeyDown(KeyCode.Return)) {
hasInteracted = true;
GetInteraction(interactedObject);
}
}
void OnTriggerExit() {
messagePanel.SetActive (false);
}
void GetInteraction(Collider interactedObject) {
interactedObject.GetComponent<Interactable> ().MoveToInteraction ();
messagePanel.SetActive (false);
}
}
PortalController.cs
public class PortalController : MonoBehaviour {
[SerializeField]
private Button button;
private Portal[] portal;
private Player player;
private GameObject panel;
void Start() {
player = FindObjectOfType<Player> ();
panel = transform.Find ("PanelPortals").gameObject;
}
public void ActivatePortal(Portal[] portals) {
panel.SetActive (true);
for (int i = 0; i < portals.Length; i++) {
Button portalButton = Instantiate (button, panel.transform);
portalButton.GetComponentInChildren<Text> ().text = portals [i].name;
int x = i;
portalButton.onClick.AddListener (delegate {
OnPortalButtonClick (x, portals [x]);
});
}
}
void OnPortalButtonClick(int portalIndex, Portal portal) {
player.transform.position = portal.TeleportLocation;
foreach (Button button in GetComponentsInChildren<Button>()) {
Destroy (button.gameObject);
}
panel.SetActive (false);
}
}
Portal.cs
public class Portal : ActionItem {
public Vector3 TeleportLocation { get; set; }
[SerializeField]
private Portal[] linkedPortals;
private PortalController portalController { get; set; }
void Start () {
portalController = FindObjectOfType<PortalController>();
TeleportLocation = new Vector3(transform.position.x + 5f, transform.position.y, transform.position.z);
}
public override void Interact() {
portalController.ActivatePortal(linkedPortals);
Debug.Log ("Interacting with Portal");
}
}
Interactable.cs
public class Interactable : MonoBehaviour {
public virtual void MoveToInteraction() {
Interact ();
}
public virtual void Interact() {
Debug.Log ("Interacting with base class.");
}
}
答案 0 :(得分:0)
可能是因为在OnTriggerStay中调用了Input.GetKeyDown。之所以要在Update中调用Input.GetKeyDown以及类似的函数,是因为该状态会在每一帧重置。尝试将其移到那里。