Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question OnTriggerEnter not working for collisions?

Discussion in 'Physics' started by habahut, Feb 24, 2024.

  1. habahut

    habahut

    Joined:
    Feb 23, 2018
    Posts:
    8
    I'm trying to understand why my colliders are not working. I have 2 objects in my scene, a players sword and a floating enemy. See attached picture for the game object configurations. These two objects are colliding, and the layer for each is configured to collide in my project physics setup. Yet I'm not seeing any OnTriggerEnter collision event.

    Here is my code for the weapon's OnTriggerEnter
    Code (CSharp):
    1.  private void OnTriggerEnter(Collider c) {
    2.      Debug.Log("weapon trigger!");
    3.      Attackable attackable = c.gameObject.GetComponent<Attackable>();
    4.      if (attackable == null) {
    5.          return;
    6.      }
    7.  
    8.      _OnTriggerEnter(c);
    9.  
    10.      attackable.TakeImpact(Impact(this.transform, c.gameObject.transform));
    11. }
    From the docs (https://docs.unity3d.com/Manual/CollidersOverview.html) I need at least one RigidBody, which I have. I don't want to simulate physics for these collisions, I just want to detect the collision and then handle it myself with character controllers and such.

    Can anyone help me understand why I'm not getting a collision event here? collision debug.png
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,252
  3. habahut

    habahut

    Joined:
    Feb 23, 2018
    Posts:
    8
    interesting. So if I don't want any physics simulation besides collision detection, what is the correct set of components for each object? I want the objects to be able to pass through each other (one is a player sword so I don't want the swing to stop when it hits an enemy). I also don't want any physics interactions like the objects pushing each other, I want to handle this myself with character controllers
     
  4. habahut

    habahut

    Joined:
    Feb 23, 2018
    Posts:
    8
    whats weird is my player character is set up the exact same, with no rigid body and an IsTrigger box collider. Yet the enemies box collider interacting with the players will result in OnTriggerEnter from being called
     
  5. codebiscuits

    codebiscuits

    Joined:
    Jun 16, 2020
    Posts:
    105
    I was really surprised to see this, so I checked it out. It's wrong [Edit: misleading, not wrong:)]!
    & it contradicts the table at the bottom here: https://docs.unity3d.com/Manual/collider-types-interaction.html
    In my test scene my dynamic rigidbody trigger cube notices when it hits my static trigger cube, and the static notices when it is being hit. Works fine for kinematic too.
    @habahut I sometimes get caught out with where I put my OnTriggerEnter script, I can never remember if it needs to be on the same GameObject that contains the Rigidbody, or the one that contains the collider (I don't think it works if it's somewhere else entirely). From your screenshot, it doesn't look like you have a script on either of the collider objects.
    The test I did just now, I had everything in the root of both objects for simplicity.
     
    Last edited: Feb 28, 2024
    habahut likes this.
  6. habahut

    habahut

    Joined:
    Feb 23, 2018
    Posts:
    8
    oh yes you are correct! I needed the actual collider to be parented to the user and not the weapon itself, so that it wouldn't move around with the attack animation. This meant the trigger was happening on this sub object and not the weapon. So I created a prefab with a simple script that calls back to the weapon for the trigger.

    thanks for the help!
     
    codebiscuits likes this.