Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Jarod99 · Jul 16, 2016 at 06:53 AM · player movementbug-perhapsbooldialoguetext box

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

60 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Player Moves During Dialogue (but scripted not to...) 0 Answers

Unity 2D Controller when active ignore another click 0 Answers

How to fix this error in New input system 0 Answers

Animation Position Shifting Player Controller Down 1 Answer

NullReferenceExeption but everything works great... 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges