Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

OnTriggerEnter2D problem, help !

Discussion in 'Getting Started' started by Xinzz, Mar 16, 2016.

  1. Xinzz

    Xinzz

    Joined:
    Jun 28, 2015
    Posts:
    67
    upload_2016-3-16_17-26-36.png

    There is 2 cubes, and both have Box collider 2D and rigidbody.

    upload_2016-3-16_17-28-23.png
    when one hit the other, why OnTriggerEnter2D is called so many times?? what's wrong??

    void OnTriggerEnter2D(Collider2D other)
    {
    Debug.Log("HIT");
    }

    And my last project seems to have the same problem, somehow i fixed it (not through coding)... and now I'm stuck again. help pls.
     

    Attached Files:

    Last edited: Mar 16, 2016
  2. Foggy-Noggin

    Foggy-Noggin

    Joined:
    Mar 6, 2016
    Posts:
    51
    I have to ask, why do you want to register a collision while your actual GameObjects are not even close to touching? Reduce your collision boxes to fit your image and try. Or am I missing something?
     
  3. Xinzz

    Xinzz

    Joined:
    Jun 28, 2015
    Posts:
    67
    upload_2016-3-17_9-6-25.png

    Same, still not working.
    And for you first question, the reason i'm doing that, is i need to use onTriggerEnter to pass on values.
    upload_2016-3-17_9-9-57.png

    For example, i need to know where the head at ,and what it points to.
     
  4. Foggy-Noggin

    Foggy-Noggin

    Joined:
    Mar 6, 2016
    Posts:
    51
    Ok, fair enough, just wanted to make sure that was intentional. I'm assuming both objects have a Rigidbody, check the Collision Detection pulldown, make it discrete. Also maybe you should check what 'Object' is in your debug call. You will probably identify the problem if you know what is colliding with what.
     
  5. Xinzz

    Xinzz

    Joined:
    Jun 28, 2015
    Posts:
    67
    I've tried.

    void OnTriggerEnter2D(Collider2D other)
    {
    Debug.Log("HIT: " + other.name);
    }

    just don't know why the function is not called one time but many times. And here's my last project.

    upload_2016-3-17_10-12-3.png

    It works fine. I don't know how i figured it out.
     
  6. Foggy-Noggin

    Foggy-Noggin

    Joined:
    Mar 6, 2016
    Posts:
    51
    Well that's great you have it working, not so much that you do know what you did to solve. Rigid body physics don't allow one object to intersect with another. If it is forced, the two objects will move themselves to solve the impossibility of two solid objects occupying the same space. On the other hand, colliders are just sensors to trigger a collision, as long as your collider is colliding it's going to fire, that's what it's supposed to do. Maybe try two colliders, one box you already have and maybe an edge collider at the upper position you need. While you will still get a collision but you can use to set a flag and ignore the remaining collisions on that collider. You can also test the collision by hand by testing the distance between your object and its destination, if it is less than a fixed amount trigger whatever you were doing in the collision routine.

    I'm real new here so maybe there is better advise to be found with the experts.
     
  7. Xinzz

    Xinzz

    Joined:
    Jun 28, 2015
    Posts:
    67
    No~~ I'm still struggling!! The working one is my last project.. Maybe i should use OnTriggerExit2D... but it means i need to change everything. I guess i have to now lol. Anyway, thanks for your help, appreciate it.
     
  8. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    1,154
    What version of unity are you using? I recall that there was one that had collider issues.

    Otherwise there must be something wrong with your code, as OnTriggerEnter2D would only be called once. How do you move the objects?
     
  9. Xinzz

    Xinzz

    Joined:
    Jun 28, 2015
    Posts:
    67
    Version is 5.3.3, i know it must be something wrong, and not coding, because it's very simple just debug.log. I just move the objects left or right.
     
  10. Xinzz

    Xinzz

    Joined:
    Jun 28, 2015
    Posts:
    67
    Or could you upload your testing project, it could be very helpful. It must be something wrong in some components in objects.
     
  11. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    1,154
    here you go...import into a 2d project + open the included scene.

    PS: i updated to 5.3.4 though...it works as expected
     

    Attached Files:

    Xinzz likes this.
  12. Xinzz

    Xinzz

    Joined:
    Jun 28, 2015
    Posts:
    67
    Oh my god, thanks to you, i found out why!! it's because i set 'Gravity Scale' to 0.. That's why!!!
     
  13. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    Say you want to make a top-down 2D racing game. How would you test if car passes the finish line if gravity has to be 0?

    No what i suspect is that you are moving the object wrong. Try changing rigidbody's position, instead of Transform's position.

    That said, in my test app i can't make OnTriggerEnter2D not work. Gravity can be 0 and i tried moving it both ways. It logged all hits.
     
    Last edited: Mar 19, 2016