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 tylersawchuk · Jun 29, 2018 at 11:37 PM · 2d gametriggergetkeydown

GetKeyDown working randomly

Ive added this script to an npc in my 2d game, so whenever im inside a trigger box collider i right click and it opens his dialogue box. and it works, but not always. Sometimes i have to click 6 or 7 times, other times it works in 1 or 2. The rigidbody on both the player and the npc are set to never sleep as i read that was a fix but it didnt seem to totally fix it, here is the code for the script attached to the npc.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class DialogieHolder : MonoBehaviour {

 public string dialogue;
 private DialogueManager dMan;

 // Use this for initialization
 void Start () {
     dMan = FindObjectOfType<DialogueManager>();
     
 }
 
 // Update is called once per frame
 void Update () {
     
 }

 void OnTriggerStay2D(Collider2D other)
 {
     if(other.gameObject.name == "Player")
     {
         if(Input.GetKeyDown(KeyCode.Mouse1))
         {
             dMan.ShowBox(dialogue);
         }
     }
 }

}

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

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by tylersawchuk · Jul 01, 2018 at 02:39 PM

I have finally found a working fix for this, thanks everyone for your help.

public string dialogue; private DialogueManager dMan; private bool playerEnter;

 // Use this for initialization
 void Start()
 {
     dMan = FindObjectOfType<DialogueManager>();

 }

 // Update is called once per frame
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Mouse1) && playerEnter)
     {
         dMan.ShowBox(dialogue);
     }
 }

 private void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.gameObject.name == "Player")
     {
         playerEnter = true;
     }
 }

 private void OnTriggerExit2D(Collider2D collision)
 {
     if (collision.gameObject.name == "Player")
     {
         playerEnter = false;
     }
 }

}

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
avatar image
0

Answer by spacekonijn · Jun 30, 2018 at 10:12 AM

this is better remove oncollisionstay2d public LayerMask thingYouWantToDetectLayer;//select the layer of the thing you want to detect in editor

// Update is called once per frame void Update() { if (Input.GetMouseButtonDown(0)){ Collider2D wathYouWantToDetect = Physics2D.OverlapBox(transform.position, new Vector2(transform.localScale.x, transform.localScale.y), 0f, thingYouWantToDetectLayer); if (wathYouWantToDetect.gameObject.name != "")//checks if the colider isn't empty { //youractions } }

}

Comment
Add comment · Show 6 · 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
avatar image spacekonijn · Jun 30, 2018 at 10:16 AM 0
Share

0 is left 1 is right 2 is scrollweelpressedin

avatar image tylersawchuk · Jun 30, 2018 at 02:19 PM 0
Share

Thanks, i gave that a try and it works way more often, but still a fair amount of my clicks dont trigger the dialogue box.

avatar image spacekonijn · Jun 30, 2018 at 03:14 PM 0
Share

how much fr

avatar image spacekonijn · Jun 30, 2018 at 03:14 PM 0
Share

how much frames do you have? maybe you can try to count your klicks

avatar image tylersawchuk spacekonijn · Jun 30, 2018 at 07:02 PM 0
Share

Im running at 140 fps

avatar image spacekonijn · Jul 01, 2018 at 11:05 AM 0
Share

remove ontriggerstay2d and use this:

 public Layer$$anonymous$$ask thingYouWantToDetectLayer;//select the layer of the thing you want to detect in editor




 // Update is called once per frame
 void Update() {
     if (Input.Get$$anonymous$$ouseButtonDown(0)){ 
         Collider2D wathYouWantToDetect = Physics2D.OverlapBox(transform.position, new Vector2(transform.localScale.x, transform.localScale.y), 0f, thingYouWantToDetectLayer);
         if (wathYouWantToDetect.gameObject.name != "")//checks if the colider isn't empty
         {
             //youractions
         }
     }
     
 }
avatar image
0

Answer by Choukri · Jun 30, 2018 at 07:09 PM

 void OnTriggerStay2D(Collider2D other)
 {
     if (other.gameObject.name == "Player" && Input.GetKeyDown(KeyCode.Mouse1))


     {
         dMan.ShowBox(dialogue);
     }
 }
Comment
Add comment · Show 1 · 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
avatar image tylersawchuk · Jun 30, 2018 at 08:09 PM 0
Share

Just gave this a shot, now the dialogue box wont open at all

avatar image
0

Answer by Tobychappell · Jul 01, 2018 at 11:13 AM

Inputs are updated at different intervals than the physics callbacks.

Consider moving the input detection to the Update method and storing the result locally.

This should give that better responsiveness feeling thingy.


From Unity Docs: https://docs.unity3d.com/ScriptReference/Input.html

"Note also that the Input flags are not reset until "Update()", so its suggested you make all the Input Calls in the Update Loop."

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

177 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 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 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

Obstacle appear when the player enter in a Trigger 1 Answer

My gameobject stops moving after entering the trigger?? 0 Answers

Display UI Panel on 2D Box Collider Trigger 0 Answers

C# GetKeyDown not working 3 Answers

Trying to get bool statement to work in OnTriggerStay2D 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