• 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 · collisionscripting problemcolliderinputcollision 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Why doesn't my prefab collide with my map? 1 Answer

Detect collision point?? 1 Answer

I am trying to create all elements in the scene from one script at run time. How do I detect collisions between the enemy and the coin without having to create another script for each item? 0 Answers

Detect polygon? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges