Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Collision Detection doesn't work

Discussion in 'Physics' started by faulknordonald, Oct 5, 2023.

  1. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    152
    I have watched 18 tutorials and trying to figure this out for 3 hours. NONE of the tutorials are accurate.

    If you don't set Is Trigger to true, then you can't enter the collider. If you set the Trigger to true, then you go right through the GameObject (because there's no physics).

    upload_2023-10-5_0-1-43.png
    Is Trigger set to true. Box no longer acts like a box.

    upload_2023-10-5_0-3-10.png
    Is Trigger is set to false. Can not enter the collider.

    I have tried EVERYTHING every tutorial has shown.

    Capsule and Box both have colliders. Capsule has Character Controller attached.

    Code for detecting collision, which is attached to the box:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DetectCollisions : MonoBehaviour
    6. {
    7.     private void OnCollisionEnter(Collision collision)
    8.     {
    9.         Debug.Log("You have Entered a Collider: " + collision.gameObject.name);
    10.     }
    11. }
    12.  
    Any help is greatly appreciated. Thanks.

    I have also tried setting a child empty gameobject with a collider so that I can enter that collider and keep the box collider the same size as the box. The player was able to enter the child gameobject just fine, but the code didn't detect that it entered the collider.
     
    Last edited: Oct 5, 2023
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    761
    Code (CSharp):
    1.  
    2.    private void OnTriggerEnter(Collider collider)
    3.    {
    4.       Debug.Log("You just entered a trigger");
    5.    }
     
    Last edited: Oct 5, 2023
  3. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    152
    OnTriggerEnter won't work because if I set it to trigger, then I can walk right through the box. I need a way to enter a collider without setting is Trigger to true.
     
  4. Deleted User

    Deleted User

    Guest

    In my current experience,
    with Unity 2021.3. "OnTriggerEnter(Collider collider)" is not very efficient. If my car runs into a collider and bounces back, the trigger doesn't work.
    " OnCollisionEnter(Collision collision)" on the other hand, works perfectly, the car doesn't even have the time to bounce back! :)
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,313
    ...It will never work as OnTriggerEnter only works for colliders with
    isTrigger
    set to true, in which they don't block collisions. Not sure what else you'd expect.

    @faulknordonald the CharacterController component will never trigger OnCollisionEnter as its designed not to intersect with colliders. You need to use OnTriggerEnter.
     
  6. Deleted User

    Deleted User

    Guest

    I knew someone might say that and that, maybe, I should specify that "is trigger" is ticked on my car's rigidbody. My answer wouldn't make any sense otherwise.
     
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,313
    You mean your car's collider? Rigidbody's can't be triggers.

    If your car's collider was a trigger than it wouldn't collide with anything. Nothing about your set up makes much sense at this point.
     
    MelvMay likes this.
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,136
    What @spiney199 just said. Nothing about the above is making sense to me either. :)

    Triggers are there to tell you that you entered the area/volume defined by a trigger (collider) and report it so you can do things, hence the name "trigger". There's no collision response. This is OnTriggerXXX callbacks.

    Collisions (non-triggers) also tell you when you contact but they have a collision response which tries to keep the colliders separated by applying impulses to the Rigidbody(2D) they are attached to. This is OnCollisionXXX callbacks.

    Rigidbody(2D) have no knowledge of triggers or non-triggers.

    The above is Unity physics 101 that is/should be shown in pretty much all starter tutorials.
     
    spiney199 likes this.
  9. Deleted User

    Deleted User

    Guest

    Yeah. I need another coffee. :)
     
  10. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    152
    Ok, I think I figured it out.

    I actually figured this out last night before seeing this. And with many Google searches, I could not find one single source telling me OnCollisionEnter doesn't work with Character Controller. All I could find was Character Controller acts like a Rigidbody.

    So, I guess I'm forced to use OnTriggerEnter? It appears to work. The only problem is, I can't set my colliders on my gameobjects. I have to create an empty gameobject. So, for ALL my triggers, I will have to create a separate gameObject. That to me sounds like a lot of extra overload.
     
  11. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,313
    It's really not. A lot of various solutions require a various configuration of game objects. A child empty with a trigger collider is pretty tame overall. And once you set up a prefab for common use scenarios it becomes less and less work over time.

    Just game dev 101.
     
  12. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    152
    Ok, thank you very much for your help.