- Home /
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!
When player enter's enemy collider, is the enemy just standing still and player moving?
I'm having some difficulty understanding your problem - If both objects have only a single collider (marked as trigger) then it's impossible for one object to be inside the trigger area of the other without the reciprocal also being true. Could you perhaps post a screenshot of your scene view with the triggers shown?
Exactly @tanoshimi, and that's the problem, I need to make sure only one of them gets the message that they got into a trigger.
Right now I'm trying @barbe63 idea(which involve triggers in children and layers), will post back if it works.
@$$anonymous$$artik1607, both of them would be moving in the end scenario, though only the player moves as of now.
Answer by barbe63 · Jul 06, 2015 at 04:56 PM
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.
Hmmmm, interesting, I didn't think about using layers, I'll try that and get back to you.
Like I told Paul, tags won't help(I already tried that) because the OnTriggerEnter would be called in either cases on both ends, which is the whole problem....
Read the whole post again. It's not only about layers but it includes also child colliders to avoid your issue. You need to have 4 colliders for a player and an enemy. Then you also need 4 scripts. The trick is for example when a player hits an ennemy range it will send messages to the player script and the enemy range script but there won't be any OnTriggerEnter in your player script to do since it will be inside the player range script. By doing all of those your problem should not happen. Try to understand it ;)
By the way the alternative consisting to check the distance would not work with OnTriggerEnter since as soon as it won't fit the dsitance requirment it would not run the test again before you exit the trigger. It would only work with a OnTriggerStay which would be very bad because of the job it would cost every FixedUpdate. $$anonymous$$y method is better for that.
Oh, I thought you already meant to use 4 colliders, so I'm trying with that(I'm struggling with raycasts + layers, that's why I couldn't get back yet =P)
Right now I only tried with the enemy getting inside the player's range, and it seems to work! I'll try with the enemy and post back the answer here ;)
Yeah, it worked great, thanks! However, I didn't need to use a rigidbody in the respective children and I didn't really get why would I need, care to elaborate? =)
Answer by Paul-Sinnett · Jul 06, 2015 at 04:50 PM
The standard way is to use tags. In the OnTriggerEnter functions, check the tags with:
void OnTriggerEnter(Collider other) {
if (other.CompareTag("Player")) {
// ...
}
}
I'm actually using that approach right now, the problem is that using tags would still yield the same problem: the player would get in the enemy's range, but not the other way around(and in both cases I do expect the tag to be "Enemy") and the player would still process as if the enemy got in range.