Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 TBART82 · Apr 05, 2016 at 05:33 AM · scripting problemcollisioncolliderinputcollision detection

2D Platformer - Action When Key Is Pressed During Collision C#

Hello, I would appreciate any help provided by anyone. My issue is that I have a Player script which is attached to the Player, and I have a separate GameObject which is an Apple. The Apple has a Box Collider 2D on it, but what i'm wondering is how can I make a Debug.Log statement when the player is during the Collision and when the Player Presses 'E', the Debug.Log Statement works, because at the moment, it is not working. Here is the Void which is attached to the Player. (Hopefully this will give you a better Idea of what I need help with).

 void OnCollisionStay2D(Collision2D coll) {
         if (Input.GetKeyDown (KeyCode.E)) {
             if (coll.collider.tag == "Item") {
                 Debug.LogError ("Player Picked Up: " + "Apple" + "!");
             }
         }
     }

So this statement does not work for some reason. I've gone through and I thought it was correct. I've made sure of things such as the Apple having the tag 'Item', but for some reason it only prints the Debug.Log statement when the 2D Player is moving and then when you rapidly press 'E', then it sometimes prints the LogError. (By the way the LogError isn't actually an error, it just helps seeing the contrast between Debug.Log statements).

All help is very much appreciated. Thank You, Kind Regards, Trent!

Comment
Add comment · Show 3
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 Ali-hatem · Apr 05, 2016 at 10:14 AM 0
Share

try this :

  void OnCollisionStay2D(Collision2D coll) {
     if (coll.gameObject.tag == "Item") {
          if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.E)) 
             print ("it work");
          }
      }

@Fredex8 edited & it work now . @TBART82 use print() or Debug.Log() .for simplicity the Debug.LogError is something else se the doc doc

avatar image Fredex8 Ali-hatem · Apr 05, 2016 at 11:41 AM 1
Share

That gives an error by the way as a collision 'does not contain a definition for tag'.

Use either if (coll.gameObject.tag == "Item") { or if (coll.collider.tag == "Item") {

Both work.

avatar image Fredex8 · Apr 05, 2016 at 11:37 AM 1
Share

The 3D equivalent of this code works fine. However the issue is that Get$$anonymous$$eyDown is only called on the frame when the key is pressed down so the player would have to release E and press it again for the debug to work. Using Get$$anonymous$$ey ins$$anonymous$$d would fix that and cause it to trigger anytime they are inside the collider and so create the effect of them walking in and picking up the apple without having to do anything else.

A boolean could then be used to cause the pickup to only happen once, or limit the number of apples picked up... whatever the desired behaviour is. However I prefer to use OnCollisionEnter and OnCollisionExit to trigger a boolean that is listened for on Update or to call a separate function. That gets around the fact that OnCollisionStay is going to be constantly called.

This should print the error every time you press E whilst inside the collider. If it is only 'sometimes' doing it then you may have an issue regarding the collision itself.

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Strange issue with collider 1 Answer

OnCollisionEnter isnt called when player lands on object 1 Answer

brick does not detect collision although ball does 2 Answers

OnTriggerEnter does not work 1 Answer

Inconsistent Collision Detection 3 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