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 /
avatar image
0
Question by gustavopinent · Jun 05, 2019 at 03:15 AM · collider2doverlap

Can't get the gameObject is inside Physics2D.OverlapBox

The situation is this: a 2D arcade game with a hero and some enemies. One attack is to jump over the enemies. I am trying to use the same code is detecting if the hero is grounded (so he can jump) to detect the hit over the head of enemies:

 testePisoPos = new Vector2(Pe.transform.position.x, Pe.transform.position.y);
         Collider2D obj = Physics2D.OverlapBox(testePisoPos, testePisoDim, 0f);
         if (obj != null)
         {
             estaNoChao = obj.gameObject.CompareTag("piso");
             if (obj.gameObject.CompareTag("demonio"))
             {
                 GameObject Hit = Instantiate(TestePos, testePisoPos, Quaternion.identity);
                 Destroy(Hit, 1f);
                 Vector2 v = this.gameObject.GetComponent<Rigidbody2D>().velocity;
                 this.gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(v.x, 2);
                 List<Collider2D> Colls = new List<Collider2D>();
                 int nColls = obj.OverlapCollider(new ContactFilter2D(), Colls);
                 for(int i=0; i<nColls; nColls++)
                 {
                     print("Herói dando toco no demônio " + Colls[i].gameObject);
                     Colls[i].gameObject.GetComponent<skeletonMove>().TocoNoCoco();
                 }
             }
         }
         else
         {
             estaNoChao = false;
         }

estaDoChao means "is grounded", happens when there is some element below with tag "piso". Pe is a child GameObject from where I will test, it is below the hero's feet. Hit GameObject is just a sprite that allows me to see if the collision is happening where it supposed to be - and it is. This code is working so far, the collision happens and the hero jumps back.

The problem is when I will get the enemy's GameObject so I can call it's TocoNoCoco() function that will make the damage in the enemy. Every time I hit the enemy's head, the print return is one GameObject that is the ground below the enemy. And, of course, there will be an erro in the following line because the ground got no TocoNoCoco() function.

The strange thing is obj.gameObject.CompareTag("demonio") matches the enemy's tag: demonio, but the obj.OverlapCollider returns a GameObject tagged as piso... whyyyyy???? How can I avoid this?

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

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

108 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

Related Questions

Multiple Collider of Platform Effector overlap Problem 1 Answer

Make colliding object not collide in overlapped area 0 Answers

Getting Collider2D.bounds.extents to update immediately (2D) 0 Answers

Overlapping colliders problem - causes player to bounce 1 Answer

Checking for Colliders with Physics2D.OverlapBoxAll Won't Work? 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