Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Drakon0168 · Dec 09, 2016 at 06:18 AM · collidertrigger

How do you detect which collider is triggered when you have multiple?

I have an enemy gameobject with 3 colliders attached to it, one is a trigger to tell it when something is in it's "sight" to target other enemies, there is another trigger to detect when something is hit by it's attacks, and a third that is not a trigger to detect when it gets hit by attacks. The problem is that I cant find a way to tell which of the two trigger colliders is being triggered with the OnTriggerEnter() method. I need it to do separate things when the sensor trigger is hit than when something is in it's attack range. I have all of the colliders in my code as variables so I can differentiate between them but I can't find a variable for the collider that calls the OnTriggerEnter() method. Here is the code for my enemy class:

 public int health, damage;
     //Linger = time the attack stays active, Delay = for the player to dodge attacks before they activate
     public float linger, delay, range, attackRange, speed;
     //Accumulation of time between updates to tell how ling it's been since an attack started
     private float timePassed;
     private Transform target;
     private bool attacking = false;
     //Two trigger colliders sensor is used to aquire a target, attackCol is used to deliver attacks
     public SphereCollider sensor, attackCol;
 
     public void Start()
     {
         attackCol.enabled = false;
         sensor.radius = range;
         attackCol.radius = attackRange;
     }
     
     public void Update()
     {
         if (attacking) 
         {
             timePassed += Time.deltaTime;
         }
 
         if (timePassed >= delay) 
         {
             attack ();
         }
 
         if (timePassed >= linger + delay) 
         {
             attacking = false;
             attackCol.enabled = false;
             timePassed = 0;
         }
 
         if (target != null && !attacking) 
         {
             attackTarget(target, Time.deltaTime);
         }
     }
 
     public void attackTarget(Transform t, float deltaTime)
     {
         //Checks if the target is in attack range
         float distance = Mathf.Sqrt (Mathf.Pow (t.position.x - transform.position.x, 2) + Mathf.Pow (t.position.y - transform.position.y, 2));
         if (distance > attackRange / 2) {
             attack ();
             attacking = true;
         } else {
             //Faces and moves toward the target
             transform.rotation = Quaternion.Euler(0, getAngle(transform.position, t.position) * (180/ Mathf.PI), 0);
             transform.position += new Vector3(Mathf.Cos(getAngle(transform.position, t.position)), 0, Mathf.Sin(getAngle(transform.position, t.position))) * speed * deltaTime;
         }
     }
 
     public void attack()
     {
         attackCol.enabled = true;
     }
 
     public void OnTriggerEnter(Collider other)
     {
         if(other.gameObject.GetComponent<Player>() != null)
         {
             target = other.transform;
         }
     }

The colliders are set in the inspector and although you cant tell the difference by name I made sure they are not the same collider listed twice alt text

And both colliders show up in the editor when the game is run

alt text

However the script cannot tell the difference between the two trigger colliders

capture.png (30.5 kB)
capture2.png (22.1 kB)
Comment
Add comment
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

2 Replies

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

Answer by atifbutt · Dec 09, 2016 at 03:33 PM

Place two child gameObjects with different tag names after that you will identify which collider its triggered

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 Drakon0168 · Dec 10, 2016 at 10:34 PM 0
Share

I was really hoping to do it all without child objects but this works too thanks

avatar image
0

Answer by RobAnthem · Dec 11, 2016 at 05:52 AM

      public void OnTriggerEnter(Collider other)
      {
          if(other.gameObject.GetComponent<Player>() != null)
          {
              if (other.collider == other.gameObject.GetComponent<Player>().attackCol)
                      target = other.transform;
          }
      }
Comment
Add comment · 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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't click gameobject when over another trigger? 1 Answer

OnTriggerEnter fails to activate 1 Answer

OnTriggerEnter firing twice 1 Answer

Enemy line of sight using linecast and colliders 1 Answer

Rigidbody Trigger 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