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 charmpie · Mar 15, 2016 at 04:11 PM · physics2dcollider2dtagbox2d

Why does Collider2D.IsTouching have no effect? C#

The script checks if a Space key is pressed. If at this moment the player is touching a ground - do jump. But it does not!

 void Update () {
         if (Input.GetKey (KeyCode.Space)) {
             isSpace = "Space Yes";
             if (GetComponent<PolygonCollider2D> ().IsTouching (GameObject.FindWithTag ("ground0").GetComponent<BoxCollider2D> ())) {
                 isTouch = "Touch Yes";
                 GetComponent<Rigidbody2D> ().AddForce (Vector2.up * jumpForce, ForceMode2D.Impulse);
             } else {
                 isTouch = "Touch No";
             }
 }

The main object is a triangle sprite tagged as "Player". It has Rigidbody2D, PolygonCollider2D, it's not Static. The second is a box (square sprite) tagged as "ground0" which has BoxCollider2D and not Static. I am also checking it with OnGUI onscreen message: Space works well, touching does not. I tried to check IsTouching separately (as below): GUI says NO it doesn't touch.

         if (GetComponent<PolygonCollider2D> ().IsTouching (GameObject.FindWithTag ("ground0").GetComponent<BoxCollider2D>())) {
             isTouch = "Touch Yes";
         } else {
             isTouch = "Touch No";
         }

I also tried to change Colliders to different types (Polygon, Edge etc.) — no effect. And I tried adding ==true to the if-statement (I think it doesn't matter). Btw, is it fine to use FindWithTag here like I do?

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 charmpie · Mar 16, 2016 at 03:24 AM

Update 1: It's my inexperience. The point is that FindWithTag finds only one object with this tag. So if we want our object to interact with every object with this tag (i.e. to jump from every platform with tag "ground"), we should use ForEach loop with FindGameObjectsWithTag.

Update 0: Some kind of solution has been found. I created an empty project with just two objects and typed the same code and it worked well. Then I went back to my problematic project. I realised that somehow I myself created the problem. Since my scene has many platforms with this exact "ground0" tag (because I duplicated them), I thought maybe a tag should be unique for every object. I tried changing the tag "ground0" to "ground" and it worked. Just to be sure that it is the case, I changed a couple of object tags with a new "ground" tag. But it still worked! Then I thought maybe the number was the problem and changed tags to "test0", and yet it worked! I can't imagine what could I do wrong, or was it really me? If you have any thoughts or encountered the same problem please reply.

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 Mnky · Aug 04, 2016 at 08:37 AM 0
Share

Thanks for the post I didn't realize that .isTouching would be looking for a specific collider ins$$anonymous$$d of returning true when touching any collider.

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

50 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

Related Questions

Physics 2D with tile collider corner problem 2 Answers

Had to turn off gravity scale, but now player goes through walls 0 Answers

Problem with hitboxes and hurtboxes 0 Answers

Top-down collisiondetection 0 Answers

2D Physics not working correctly 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