Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Help, 2d unity triggers not working

Discussion in '2D' started by EX_34, Dec 6, 2016.

  1. EX_34

    EX_34

    Joined:
    Jan 31, 2016
    Posts:
    11
    Hello,

    Im in school for Game design, and as a side project I have been making Pacman in unity 2d. Everything about my pacman game is flawless so far, except my pac dots dont work. For some reason, checking "is trigger" is not turning off the physics collision detection, and my pacman walks right into them and stops like its a wall.

    I have code that uses OnTriggerEnter to destroy the pac dots and add 1 to the score, but since my pacman cant enter the collider, the dots just act as walls. For some reason when I check "is trigger" on the pac dots, they still retain their physics collision detection.

    Can someone help me figure out why this is happening?
     
  2. capnjake

    capnjake

    Joined:
    Nov 12, 2013
    Posts:
    53
    Is it possible you can screenshot your inspector window for the dots?
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,546
    Check to see if you're getting an OnCollisionEnter2D callback instead. If so, you're either not setting it to be a trigger or another collider is stopping it getting to the trigger in question.
     
  4. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    Make sure You players has a rigidbody2D and a collider2D(circle as it pacman), your dots or pills as they are officially called will also need a circlecollider2D you MUST only use 2D or 3D, you CANNOT mix these together it will not work.
     
  5. EX_34

    EX_34

    Joined:
    Jan 31, 2016
    Posts:
    11
    here is the screen shot requested.

    I have a rigidbody2d on on my packman, and my pacman works perfectly. He collids with walls, and the animations work in all directions.

    While the collider in the photo is a box collider, I have also tried it with circle colliders. My Pacman runs into them like its a wall. Hitting is trigger does nothing sadly.
     

    Attached Files:

  6. EX_34

    EX_34

    Joined:
    Jan 31, 2016
    Posts:
    11
    I think my issue may be the fact that i made this on my mac. I usually do my unity work on my PC.

    Ive even tried just putting an empty game object into the scene, making a circle collider on it and checking is trigger. It still acts as a wall even with just a trigger collider selected.
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,546
    If you can provide me with the project then I'd be happy to take a look at it for you. Either provide me with a download link or submit a bug case and give me the number.
     
  8. EX_34

    EX_34

    Joined:
    Jan 31, 2016
    Posts:
    11
    I will get you a project as soon as I can. I have transfered most of the project over to my PC to test to see if it was the mac, and I get the same issue. Even when I put an empty object into the scene and put a box or circle collider on it, if I check is trigger my pacman still collides with it.
     
  9. EX_34

    EX_34

    Joined:
    Jan 31, 2016
    Posts:
    11
    Here is the package on my mac, its more complete than my PC version. I have a trigger test thats just an empty object with a collider attatched, but checking the istrigger button doesnt do anything for me.
     

    Attached Files:

  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,546
    It works fine here. Could you let me know exactly which version you're using. I've never seen a trigger not be set to a trigger.

    Pacman Video
     
  11. RockyWallbanger

    RockyWallbanger

    Joined:
    Mar 16, 2014
    Posts:
    85
    The problem is in your PacManMove script, specifically in your valid method. This line
    Code (CSharp):
    1. return (hit.collider == GetComponent<Collider2D>());
    is returning true only when the first collider your linecast hits is the pacman. When you linecast and hit a pacdot your valid method is returning false (and your destination isn't valid) because the first thing it hits isn't itself. I'm curious to know why you're not relying on unity's physics to deal with maze collision?
     
  12. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,546
    This is correct, sorry!

    It's work fine here because I wasn't using an official Unity version; I just used a branch I'm experimenting with, oops!
     
    Last edited: Dec 8, 2016
  13. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,546
    I would recommend doing the follow:

    First, turn off Physics2D.queriesHitTriggers in the Physics2D settings. This way, your check which uses a query won't detect triggers.

    Second, you could simplify the query to something like:
    Code (CSharp):
    1.     bool valid (Vector2 dir)
    2.     {
    3.         Vector2 pos = transform.position;
    4.         return Physics2D.OverlapPoint (pos + dir) == null;
    5.     }
    6.  
     
  14. EX_34

    EX_34

    Joined:
    Jan 31, 2016
    Posts:
    11
    Thank you! That worked, I can now pick up my pills. I didnt know you could toggle the bool that way, ill make sure to remember that! I was just following a tutorial I found on google. Guess the code was bad.
     
  15. EX_34

    EX_34

    Joined:
    Jan 31, 2016
    Posts:
    11
    I was just following a guide online, I guess the code was bad. After removing that check and just using the basic collision, I found that this also made the triggers work properly but then when my pacman hit a wall, it would get stuck on it. I suspect thats why the guide was using the line cast I guess, cause it avoided that problem.

    I would love to know how to do it without a line cast, that would be awesome too, but like I said I am still in school in learning. Part of why I did this was so I could better understand the basic fundamentals of how movement and stuff works since my school always gives us templates with that stuff included.