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
2
Question by MS77Lieger · Jul 20, 2017 at 10:26 PM · c#unity 5colliderprefabtrigger

How do you trigger a specific collider using OnTriggerEnter?

Hey, guys, I'm new to unity, and C# in general, but I am wanting to have a prefab, which has 2 colliders; use one for general detection of triggers, and one specifically for the use of another prefab's OnTriggerEnter. Any help is welcome, and I hope to hear from you soon!


 void OnTriggerEnter(Collider particles)
 {
     Flight();
     //Destroy(gameObject);
 }



Comment
Add comment · Show 1
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 No_Username_Found · Sep 01, 2021 at 03:49 PM 0
Share

I know its old, but for those who wondered in from Google:

 public void OnCollisionEnter(Collision c)
 {
      //this is the rigidbody that was collided. This will be a parent object if that is where the rigidbody is assigned.
      Debug.Log("Collided with: " + c.transform.name);
 
      //this is the collider that was collided. It will be the  transform which contains the collider and will NOT be a parent
 Debug.Log("Specifically, the part collided with was " + c.collider.transform.name);
 }

So a parent should have a rigidbody. It can have 1 collider on it. Then add a child transform and add a collider to the child. There can be multiple children, each with their own collider.

The children are automatically part of the rigidbody in their parent.

Collision.transform will refer to the parent which has a rigidbody. Collision.collider.transform will refer to the child which has a collider.

2 Replies

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

Answer by a161803398874 · Jul 29, 2017 at 07:24 AM

@MS77Lieger Hi , you should use the onCollisionEnter method, you can use several properties to make shure you can trigger the exact collider you want, for this method to work you need to set correctly the properties of the object you want to collide with... first you need to make shure that the check mark in the Collider Properties (inspector window) that says "IsTrigger" is checked ,this is so the trigger method can detect it...When this is checked objects can go thru this collider not hiting with him, so you may need to put two colliders on the object makin the "IsTrigger" one bigger than the normal one so the system can detect the collider... Next you need to decide on how you are going to detect the collision, this can be with "GameTag" "Name" or others... @Goldiedog123 gave an example using "Gametag" we will use the "Name" of the game object.

  void OnCollisionEnter(Collision dataFromCollision)
  {
          if (dataFromCollision.gameObject.name == "GameObjectName")
          {
             //Do whatever you want 
          }
        }
      }


Look that when you get the data from collision is in a "Collider" type and you acces the gameObject properties of this collider...... one of the game object properties is "name" others are "CompareTag" ,"GetComponent", "GetComponentInChildren" and so on

you can also acces other properties of the collider itself

 Vector3 triggerPosition = new Vector3 (1.0f,2.3f,4.3f); // Storing some position in space

     void OnCollisionEnter(Collision dataFromCollision)
     {
 
 
     if (dataFromCollision.transform == triggerPosition){
         //Not very useful but here it does something when you collide with something in this exact triggerPosition
     }
 }


I truly recomend you to use this method, is one of unity´s most powerful tools. you can activate a scene when we pass thru a collider or maybe make an explosion, a jump scare, a portal when we hit a door with a special tag, a respawn if you get out of level.... etc

Comment
Add comment · Show 2 · 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 MS77Lieger · Jul 29, 2017 at 09:26 PM 0
Share

This sounds great, thanks for the tip, and yeah that makes Goldidog123's suggestion make a lot more sense then. Cool, I will give it a try and let you know if it works!

avatar image MS77Lieger MS77Lieger · Jul 29, 2017 at 11:30 PM 0
Share

Thank you very much, this worked out great, tags were definitely the issue, thanks for helping to clear that up, I've been bashing my head against a wall on this for hours.

avatar image
5

Answer by Goldiedog123 · Jul 21, 2017 at 01:44 AM

HEY,

Okay so you want one to only collide with certain objects?

The bellow script will allow the gameobject with the script attached to ignore objects with a certain tag.

 void OnCollisionEnter(Collision collision)
 {
         if (collision.gameObject.tag == "WHATEVER")
         {
             Physics.IgnoreCollision(collision.collider, GetComponent<Collider>());
         }
       }
     }

Hope this helps

Comment
Add comment · Show 4 · 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 MS77Lieger · Jul 21, 2017 at 09:02 PM 0
Share

I'll give this a try, thank you very much!

avatar image MS77Lieger · Jul 21, 2017 at 09:28 PM 0
Share

Unfortunately, this didn't seem to work for me. it wasn't triggering anything and tried switching collisions to trigger as well, and that just had errors riddled everywhere.

avatar image Goldiedog123 MS77Lieger · Jul 24, 2017 at 04:59 AM 0
Share

did you set up tags? this script ignores collisions with gameobjects with certain tag

avatar image MS77Lieger Goldiedog123 · Jul 28, 2017 at 11:28 PM 0
Share

Right, and I get that, however, it's the exact opposite of what I'm going for, looking to collide with 1 specific thing.

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

393 People are following this question.

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

Related Questions

Spawned tree not falling 1 Answer

How to access a prefab's boolean property through collision (with the same Prefab but different Instance)? 1 Answer

If statement failing OnTriggerEnter 0 Answers

OnTriggerEnter Not Working 1 Answer

Erratic trigger behavior, I can't find the fault. 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