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

Question 2D Trigger collider not working correctly

Discussion in '2D' started by stealthyshiroean, Mar 20, 2023.

  1. stealthyshiroean

    stealthyshiroean

    Joined:
    Jun 29, 2021
    Posts:
    6
    So, I've set up a pitfall in my game that has a trigger collider. The player has four directional colliders at their feet to determine which way they fall to make it look like they fell into the pit.

    Code (CSharp):
    1. if (!didFall)
    2.             {
    3.                 switch (collision.gameObject.name)
    4.                 {
    5.                     case ("TopFall"):
    6.                         didFall= true;
    7.                         playerAnimator.Play("Player_TopFall");
    8.                         print("top");
    9.                         break;
    10.                     case ("BottomFall"):
    11.                         didFall = true;
    12.                         playerAnimator.Play("Player_BottomFall");
    13.                         print("bottom");
    14.                         break;
    15.                     case ("RightFall"):
    16.                         didFall = true;
    17.                         playerAnimator.Play("Player_RightFall");
    18.                         print("right");
    19.                         break;
    20.                     case ("LeftFall"):
    21.                         didFall = true;
    22.                         playerAnimator.Play("Player_LeftFall");
    23.                         print("left");
    24.                         break;
    25.                 }
    26.             }
    However...it doesn't seem to always work properly. For instance, from these images, you can see that the "right" collider was the one that passed into the trigger yet for some reason it says it was the "top", but they didn't even touch...I'm very confused not sure what's going on. So I guess, it might not be the trigger collider but could be the code itself.

    Any insight would be much appreciated! Please let me know if any more info is needed.
     

    Attached Files:

  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    There's not been a fundamental bug in collision detection for as long as I can remember. Best not to assume first that Unity is at fault and look at your code.

    Make sure the names are correct for the GameObjects containing those boxes? If it were me, I'd move it around manually and see what is reported. Also, do a quick test in a new project in-case it's something you're doing in this one. Should only take 5 mins.

    Beyond that, the snippet above isn't really enough to determine anything.

    I would say that I would steer away from hardcoded names and just get a reference to the four colliders in your script. Then you only have to compare the incoming object and no names are involved.
     
  3. stealthyshiroean

    stealthyshiroean

    Joined:
    Jun 29, 2021
    Posts:
    6
    Sorry, didn't mean to presume that there was an issue with Unity because I definitely thought it was my own error haha. I'm pretty beginner when it comes to this stuff.

    The names are definitely correct though. I'll switch them over to references. Should have been something I did a long time ago but that was the solution I came up with at the time.

    Thanks for the help! Hopefully everything works after a bit more troubleshooting.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    No problem. I'm not saying there isn't an issue internally, it's just that one thing is more likely than the other; I don't want to sound like I'm discounting that.

    Regardless, I'd be very curious to know what the issue turns out to be. If you'd not mind posting back here or DM-ing me, that'd be great.

    Thanks!