Search Unity

OnTriggerEnter somehow only fires when my MainCharacter enters

Discussion in 'Editor & General Support' started by HeaDiii, Oct 30, 2018.

  1. HeaDiii

    HeaDiii

    Joined:
    May 18, 2015
    Posts:
    61
    Hey there,

    I know how stupid this sounds, but I just can't figure out what I am doing wrong.

    I have a GameObject that only has a Transform Component, one SphereCollider which has checked IsTrigger and one simple Script:

    Code (CSharp):
    1.  
    2. public class ColliderTrigger : MonoBehaviour
    3. {
    4.    public EntityType type;
    5.    
    6.    private void OnTriggerEnter(Collider other)
    7.    {
    8.       Debug.Log("Something entered: " + other.name);
    9.       ICollidable collidable = other.GetComponent<ICollidable>();
    10.       if (collidable == null)
    11.       {
    12.          return;
    13.       }
    14.       if (collidable.EntityType == type)
    15.       {
    16.          transform.parent.SendMessage("ColliderEnter", collidable);
    17.       }
    18.    }
    19.  
    20.    private void OnTriggerExit(Collider other)
    21.    {
    22.       Debug.Log("Something exited: " + other.name);
    23.       ICollidable collidable = other.GetComponent<ICollidable>();
    24.       if (collidable == null)
    25.       {
    26.          return;
    27.       }
    28.       if (collidable.EntityType == type)
    29.       {
    30.          transform.parent.SendMessage("ColliderExit", collidable);
    31.       }
    32.    }
    33.  
    34.    private void OnDrawGizmos()
    35.    {
    36.       switch (type)
    37.       {
    38.          case EntityType.Interactable:
    39.             Gizmos.color = Color.blue;
    40.             break;
    41.          case EntityType.Enemy:
    42.             Gizmos.color = Color.red;
    43.             break;
    44.       }
    45.       Gizmos.DrawWireSphere(transform.position, GetComponent<SphereCollider>().radius);
    46.    }
    47. }
    48.  
    When I start the game, "Something entered: Vanguard" is immediately printed on the console (Vanguard being the name of my guy). If I move the collider away ("Something exited: Vanguard" is printed) and then back to the position it should be, "Something entered: Vanguard" is printed again. Nothing else in my whole scene can call OnTriggerEnter somehow. I checked all references in the inspector, the sizes of my colliders, the colliders on other objects and basically everything I could think of. Changing the radius of the trigger to 5000 also doesn't get me to find other objects.

    As I began prototyping my game, I started off with Ethan and used just the same structure which worked beautiful. Now I created the new Vanguard Character, copied the old GameObjects from Ethan, corrected my references and somehow it is just not working. The only difference between Ethan and Vanguard is that Ethan used the default way to move the character (Rigidbody, CapsuleCollider, ThirdPersonUserControl and ThirdPersonCharacter) while my guy uses a CharacterController and one PlayerMovement-Class that I wrote. But I doubt there is any problem with that part.

    Can someone point out what I am missing? I have no clue whats going on.

    I have 2 videos to make it more clear. One with my guy, one with ethan. Both using the same ColliderTrigger-Component from above with the same configuration.

    Working with Ethan: http://sendvid.com/pd87t15x

    Not working with my dude: http://sendvid.com/48w5zcjr
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,634
    Do your other objects have rigidbodys attached? In order for OnTriggerEnter to work, I think that at least one of the objects needs a rigidbody component.
     
    HeaDiii likes this.
  3. HeaDiii

    HeaDiii

    Joined:
    May 18, 2015
    Posts:
    61
    I just added a Rigidbody Component to my character and now it works. I don't know why OnTrigger-Functionalities depend on a Rigidbody on a parent-transform but okay.

    Thanks
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,519
    Because Triggers an Collisions are calculated by the Physics system and the physics system is aware of Rigidbodies.
     
    Joe-Censored likes this.