GetKeyUp/GetKeyDown optimization
Hello everyone.
I am currently having trouble with GetKeyUp and GetKeyDown. I think this is a bug, it is not game breaking but is really annoying if someone were to try to go through the game quickly.
I want the Player to be able to interact with NPCs. When the Player comes into a 2D Collider around the NPC and presses Space, a text box appears. When the text box is on the screen, the Player is unable to move. When the Player presses Space again, the text box disappears and he is able to move again.
While the whole script works, there are some strange things about it.
Here's what's happening.
If I open the text box once and close it, I must move my character around a bit before the Space key works again. If I do not move, the Space key does not toggle the text box on and off anymore. I actually like this "bug-like feature thing", it helps prevent the Player from spamming the Space key, I guess.
But there's something bothering : when the text box is on the screen, and the Player holds down a movement key and then presses the Space key, the Player will move a bit, and get stopped with the text box appearing again. If the Player tries to close the text box while still holding the movement key, he'll move forward a bit again, and get stopped by the text box again until he is out of the 2D Collider.
I am using "GetKeyUp" to open the text box, and "GetKeyDown" to close it, to avoid it opening and closing instantly. I am using a "OnTriggerStay2D" function to detect whether the Player is next to the NPC or not.
I have tried using "GetKeyUp" on both and "GetKeyDown" on both, but the text box doesn't appear at all (probably because it opens and closes it at the same time).
I would like to know if there is another way to toggle a bool with the same key without using the GetKeyUp / GetKeyDown method, and if there is a way to make it so that the player doesn't get caught into this weird bug when trying to move away from a NPC as soon as they close the text box.
Here's my code, but I don't think it is a bug because the script works perfectly if I never hold a movement key and close the text box at the same time.
First code, the text box is a child of the GameObject this script is attached to :
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class DialogueManager : MonoBehaviour {
public Animator dBox;
public Text dText;
public bool dialogueActive;
public PlayerMovement allowPlayerMovement; //When set to false, Player speed = 0
void Start ()
{
dBox = GameObject.Find ("TextBox").GetComponent<Animator> ();
dialogueActive = false;
allowPlayerMovement = GameObject.Find ("Player").GetComponent<PlayerMovement> ();
}
void Update ()
{
if (Input.GetKeyDown (KeyCode.Space) && dialogueActive)
{
dialogueActive = false;
dBox.SetBool ("DialogueActive", false); //Makes the text box disappear
allowPlayerMovement.canMove = true; //Sets Player speed to 52
}
}
public void ShowBox(string dialogue)
{
dBox.SetBool ("DialogueActive", dialogueActive); //Makes the text box appear
dText.text = dialogue;
}
}
Second code, attached to the NPCs :
using UnityEngine;
using System.Collections;
public class DialogueHolder : MonoBehaviour {
public string dialogue;
public DialogueManager dManager;
void Start ()
{
dManager = FindObjectOfType<DialogueManager> ();
}
void Update ()
{
}
void OnTriggerStay2D(Collider2D other)
{
if (other.gameObject.name == "Player")
{
if (Input.GetKeyUp (KeyCode.Space))
{
dManager.dialogueActive = true;
dManager.ShowBox (dialogue);
dManager.allowPlayerMovement.canMove = false; //Sets Player speed to 0
}
}
}
}
The // are not in my actual script, I added these to make sure everything was comprehensible. Sorry for the long message.
Can this be fixed, or is this something I'll have to cope with?
Thanks in advance for the help.
Answer by Jarod99 · Jul 17, 2016 at 01:25 AM
I've spent the whole day trying to change the code and adapt it to my needs, to no avail. I will use a plugin for dialogue management within my project.
I would still like to know if there is a way of making sure that this issue doesn't happen again with something else. I have thought of using a coroutine that would wait for a given amount of time before the space bar is usable again, to avoid it happening as much as possible, but this wouldn't solve the problem entirely and I couldn't get it to work. I have also thought about disabling the text box once the message has been displayed, but this can't work every time, and is not really optimized.
If you guys know a way of avoiding this kind of issues, or something that could help me in the future about this, please let me know.