Search Unity

Question 2D Collision is not being detected [Solved]

Discussion in '2D' started by PixelPopperProductions, Aug 28, 2020.

  1. PixelPopperProductions

    PixelPopperProductions

    Joined:
    Aug 16, 2020
    Posts:
    2
    Hello!

    I have spent far too long scratching my head wondering what check box is wrong or what I am missing with this issue so I have come for some help. As far as I can tell, all of the collision and rigid bodies are set up correctly. (he says with much ignorance)

    Currently all I am trying to do is get a debug string to print when a collision is detected between the projectile and my test collision. I have attached my set up.

    Stuff I have tried:
    * Making both the projectile and collision bigger
    * Remaking all assets
    * Experimenting with different collision settings (Dynamic, Static..)
    * Moving ordering of collision and rigid bodies (I know... probably does nothing...)

    Can someone tell me what stupid thing I am doing wrong please :confused:

    Collision Script:
    Code (CSharp):
    1. public class CollisionController : MonoBehaviour
    2. {
    3.     // Start is called before the first frame update
    4.     void Start()
    5.     {
    6.      
    7.     }
    8.  
    9.     // Update is called once per frame
    10.     void Update()
    11.     {
    12.      
    13.     }
    14.     private void OnCollisionEnter2D(Collision2D collision)
    15.     {
    16.         Debug.Log("Collision Detected");
    17.     }
    18.  
    19.     private void OnCollisionStay2D(Collision2D collision)
    20.     {
    21.         Debug.Log("Collision Detected");
    22.     }
    23. }
    Player Projectile Prefab:
    Player_Proj.png

    Test Collision Prefab:
    TestCollision.png
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @PixelPopperProductions

    You have marked your objects as triggers, so they won't collide, but only trigger those trigger events...

    So simply put, use OnTriggerEnter2D and OnTriggerStay2D methods instead of OnCollisionEnter2D and OnCollisionStay2D if your object needs to be a trigger.

    Otherwise, if you want your bullet (for example) to collide with other objects, uncheck the "Is Trigger" checkbox in your collider settings.

    Here are the pages from the manual:
    https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionEnter2D.html
    https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter2D.html
     
  3. PixelPopperProductions

    PixelPopperProductions

    Joined:
    Aug 16, 2020
    Posts:
    2
    Yup... just like I thought... simple fix.

    Thank you so much!
     
    eses likes this.
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,499
    Also note: If you don't want a body to rotate or move then don't just activate all the constraints. Instead, set it to Static body-type (not Dynamic).
     
    Ceperadlo likes this.
  5. dragonitemaster876

    dragonitemaster876

    Joined:
    Mar 31, 2021
    Posts:
    3
    I was wondering as well, so I looked up "collision not working" and your post's first sentence of "what check box is wrong" reminded me that I hadn't set anything as a trigger. thx