Check which trigger called OnTriggerEnter

So, in the game I’m making I’m trying to have the enemies’ and the player’s to have a specific logic when one gets in range of the other.

The catch is that the enemy and the player have different ranges to do whatever they do, and to do that, I’m assigning a Trigger for each object with different ranges, the problem is that when the player(who has a lower range) gets into the enemy trigger(which has a bigger range) the OnTriggerEnter is called on both ends.

The problem is that I want OnTriggerEnter to be called ONLY in the object the owns that trigger, is there a way to check that sort of thing?

To better illustrate here’s what I mean:
What I want:

    //Enemy script
    void OnTriggerEnter(){
         //Do enemy thingy whenever a player gets into the enemy's range
    }
    
    //Player Script
    void OnTriggerEnter(){
         //Do player thingy whenever an enemy gets into the player's range
    
    }

What actually happens:

    //Enemy script
    void OnTriggerEnter(){
         //Do enemy thingy whenever a player gets into the enemy's range or in the player's range
    }
    
    //Player Script
    void OnTriggerEnter(){
         //Do enemy thingy whenever a player gets into the enemy's range or in the player's range
    }

I thought about calculating distances whenever the Trigger event was called, but I really hate the unity units system, and I’d prefer if I could get away without that =)

Thanks!

You have actually 2 problems, the one you said and the fact that your events actually trigger when your both ranges collide.

Here is a little sheme to explain what I mean.

So to deal with that you need to set the range to be child colliders that will ideally only collide with the character using the physics matrix. This way should also prevent your first issue.

I will summarize the steps you need to help a bit more.

  • Make a character collider that approximate the shape of it (a capsule should be fine), attach rigidbody to it and make them kinematic if you don’t use forces to move your characters. Assign them a layer for best practice (call them player and enemy).
  • Make a range collider as child for both of them. Attach also a rigidbody to it so it won’t act as a compound collider with your character. Assign them a different layer too(call them player range and enemy range)
  • Go in physics settings and uncheck all collisions for those 4 layers except the player range/enemy and enemy range/player.
  • Make separate scripts “PlayerRange” and “EnemyRange” attached to the range gameobjects and put your code to send messages there. I use to make the main player script a public variable on my sight script so after I assign it in inspector it can access it very easily.

Note: you can also deal only with tags instead of separate layers or both of them but I think the way I provided you is the best practice to avoid unnessary collisions.

The standard way is to use tags. In the OnTriggerEnter functions, check the tags with:

void OnTriggerEnter(Collider other) {

    if (other.CompareTag("Player")) {

        // ...
    }
}