Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by MiguelJD23 · Jul 06, 2015 at 04:13 PM · c#trigger

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!

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kartik1607 · Jul 06, 2015 at 05:39 PM 0
Share

When player enter's enemy collider, is the enemy just standing still and player moving?

avatar image tanoshimi · Jul 06, 2015 at 07:00 PM 0
Share

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?

avatar image MiguelJD23 · Jul 06, 2015 at 08:15 PM 0
Share

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.

avatar image MiguelJD23 · Jul 06, 2015 at 08:16 PM 0
Share

@$$anonymous$$artik1607, both of them would be moving in the end scenario, though only the player moves as of now.

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

alt text

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.


colliders.jpg (46.6 kB)
Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MiguelJD23 · Jul 06, 2015 at 06:43 PM 0
Share

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....

avatar image barbe63 · Jul 06, 2015 at 07:10 PM 0
Share

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.

avatar image MiguelJD23 · Jul 06, 2015 at 08:13 PM 0
Share

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)

avatar image MiguelJD23 · Jul 06, 2015 at 08:25 PM 0
Share

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 ;)

avatar image MiguelJD23 · Jul 06, 2015 at 09:40 PM 0
Share

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? =)

Show more comments
avatar image
0

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")) {
 
         // ...
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MiguelJD23 · Jul 06, 2015 at 06:20 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

How to enable gravity on gameobject when interact by player 3 Answers

Trigger Activated Multiple Times From One Click 2 Answers

Guides or Tutorials for a Very Basic Enemy AI? [Unity 5] 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges