Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 mah_0003 · Mar 02, 2015 at 01:39 PM · c#collidertriggerturret

Using two trigger colliders

As the title says, I am needing help with two trigger colliders. I am creating a co-operative puzzle game about two cubes, a big cube and a small cube. In this game I am creating a turret that finds and shoots the cube. However, I want the big cube to be detected anywhere around the turret in a circular range (currently working) and the small cube to only be detected in a smaller cube range in front of the turret (not working). In a sentence, my problem is I am trying to get input from the different triggers separately, and currently just using OnTriggerStay/Enter/Exit only detects the big circle collider (as it is the biggest). Here is a picture if it helps. Notice the big collider, then the small one. alt text

Thank you if you can help, and sorry if my explanation is a little vague, also I work in C# but java is fine if that is all you know. I already made some code for detecting the big cube, as seen below, but I need a way to detect them separately. (I have edited it a bit to show you more or less how the small cube should function.)

 public Transform target;
 public Transform target1;
 public GameObject barrel;
 public bool inrange;

 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update() {

 }

 void OnTriggerEnter(Collider other) {
     if (other.tag == "Player1") {
         inrange = true;
         barrel.particleSystem.Play ();
     }

     if (other.tag == "Player2") {
         inrange = true;
         barrel.particleSystem.Play ();
     }
 }

 void OnTriggerStay(Collider other) {
     if (other.tag == "Player1") {
         transform.LookAt (target);
     }

     if (other.tag == "Player2") {
         transform.LookAt (target);
     }
 }

 void OnTriggerExit(Collider other) {
     inrange = false;
 }

}

untitled.jpg (55.2 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

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by curiouspers · Mar 02, 2015 at 10:41 PM

As far as i know you can't get data about what trigger triggered the action, and it's not recommended to use more than 1 trigger on one gameObject.

However there is a workaround:

You may attach your triggers to 2 child gameObject of your turret. Then make a simple script wich onCollisionEnter will call custom function of your turret script with 2 params, the object that calling this function and Collider information that has triggered it.

This should work, but you want to modify it for you:

Here i assuming that children gameObjects is named TriggerRound and TriggerFront.

// on child gameObject with trigger

 public class TurretChildScript : MonoBehaviour {
     private TurretScript parentScript;
     void Start(){
         parentScript = transform.parent.GetComponent<TurretScript>();
     }
     void OnTriggerEnter(Collider other){
         parentScript.recieveTriggerEnter (name, other);
     }
 }

//and on parent gameobject (turret):

 public class TurretScript : MonoBehaviour {
 
     public void recieveTriggerEnter(string fromObject, Collider other){
         if(fromObject == "TriggerRound"){
             if (other.tag == "Player1") {
                 transform.LookAt(other.transform);
                 Debug.Log(fromObject);
             }
         }
         if(fromObject == "TriggerFront"){
             if (other.tag == "Player2") {
                 transform.LookAt(other.transform);
                 Debug.Log(fromObject);
             }
         }
     }    

 }
 

If you want OnTriggerStay or OnTriggerExit, you can add it in both scripts

If this was helpfull please upvote the answer and mark it as checked. Best regards!

Comment
Add comment · Show 5 · 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 mah_0003 · Mar 02, 2015 at 11:47 PM 0
Share

I think this will work. I'll try it out as soon as I finish school! Thank you!

avatar image curiouspers · Mar 04, 2015 at 01:03 PM 0
Share

@mah_003 How is it going? Was it helpfull?

avatar image RogerGu · Jan 12, 2016 at 10:18 PM 0
Share

Exactly what I needed. Thank you.

avatar image Goldark · May 23, 2016 at 07:53 AM 0
Share

Thanks, it works!

avatar image thaMorganic · Sep 23, 2016 at 10:08 PM 0
Share

I was trying to control 2 triggers on 2 different objects at the same time and was stuck for like an hour. Your answer helped alot so thank you so much!

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

7 People are following this question.

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

Related Questions

Weird behaviour with OnTriggerEnter 0 Answers

Ideas for detecting collision speed with trigger? 2 Answers

Trigger collider information 3 Answers

If I deactivate the collider and then reactivate it, the trigger no longer works? 1 Answer

On Method for a collider on another object. 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