Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by EsC369 · Aug 13, 2020 at 03:54 PM · scripting problemcollidersienumeratorkeyboard input

Issue With Input.GetKey and IENumerator in Grabbing Script

So what Im trying to achieve here, is when the player holds down E it continously grabs the item and grabs certain increments per swipe. Now the way I set this up so far, is just to log a string of "Grabbing Item" In this case lets focus on 1 item, a large stack of money. My hope, was when the player is within the colliders of lets say (Money Stack), and if I am holding down E, then every X seconds, the Log should say, "Grabbing Money".

Now it does do this for sure, but its weird.. Lets say we set the EINumerator to yield for about 3 seconds.How its currently playing out, is it waits the 3 seconds but then BOMB! does the log about 20-30 times while im holding down E. If I am to just tap E, it literally does it like 10 times really fast.

Why the heck, is my code playing out this way? My theory is the logic I have for the collider.. I think that every split second my player is within the collider, it runs the IENumerator function multiple times within a second. This is the only thing I can think of. But at the same time, I'm not to sure of the proper approach/architecture I should be taking? Any advice is appreciated, thanks.

Code:

 public class GrabMoneyStack : MonoBehaviour
 {
     // Variables:
     [SerializeField] public int moneyAmt;
     [SerializeField] public float grabSpeed;
 
     public void OnTriggerStay(Collider other) 
     {
         // check if its the player:
         if(other.tag == "Player")
         {
             // Check to see if user HOLDS DOWN the "E" key:
             if(Input.GetKey(KeyCode.E))
             {
                 StartCoroutine(GrabMoney());
             }
         }
     }
     IEnumerator GrabMoney()
     {
         // yield return new WaitForSeconds(Time.deltaTime * grabSpeed);
         yield return new WaitForSeconds(grabSpeed);
         Debug.Log("Grabbed Money!");
     }
 }
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
Best Answer

Answer by RaptorRush · Aug 13, 2020 at 06:01 PM

“I think that every split second my player is within the collider, it runs the IENumerator function multiple times within a second.” Yes, that’s seems to be the problem.

To fix it, just make sure that the coroutine has ended before calling another one.

A simple bool is enough:

 public class GrabMoneyStack : MonoBehaviour {
     // Variables:
     [SerializeField] public int moneyAmt;
     [SerializeField] public float grabSpeed;
     private bool grabbingMoney;
     public void OnTriggerStay(Collider other) {
         // check if its the player:
         if (other.tag == "Player") {
             if (Input.GetKey(KeyCode.E) && !grabbingMoney) { // start only is grabbingMoney == false
                 StartCoroutine(GrabMoney());
             }
         }
     }
     IEnumerator GrabMoney() {
         grabbingMoney = true;
         yield return new WaitForSeconds(grabSpeed);
         Debug.Log("Grabbed Money!");
         grabbingMoney = false;
     }
 }


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 EsC369 · Aug 13, 2020 at 11:23 PM 0
Share

Awesome! Thank you very much! I thought of doing just this, but I had my placement wrong with the setting of said bolean transition. Thanks!

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

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

I want to make the gameobject rigidbody.iskinematic=true for 3s and immune to damage when it triggers the collider and then false again 0 Answers

physics.OverlapSphere colliders 1 Answer

Get the triangles of a convex mesh collider 1 Answer

Rigidbody trapped in collider after collision 1 Answer

How to check if object are link via other collider 0 Answers


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