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
0
Question by kfchristian15 · May 30, 2015 at 03:44 AM · c#collisionphysicscolliderignorecollision

Physics.IgnoreCollision not working

So I have the following script on a few empties with box colliders set over water so that if you are flying, you cannot fly over the water, but you can walk into the water. It works great for the character, however other objects will still collide with it. I've put Debug.Log on the script and I'm getting them to register and no errors are showing. Please let me know if you see a reason for why other object would still collide with the object this script is on or another way to do this if not. Thanks!

 public GameObject text;
 public GameObject player;
 Collider detector;

 void Start () {
     detector=GetComponent<Collider>();
 }

 void OnCollisionEnter (Collision col)
 {
     if (col.gameObject==player)
     {
         if (player.GetComponent<ThirdPersonCharacter>().hovering)
         {
             text.SetActive (true);
         }else {
             Physics.IgnoreCollision (player.GetComponent<CapsuleCollider>(),GetComponent<Collider>());
         }
     }else{
         if (col.gameObject.GetComponent<Collider>() !=null)
         {
             Physics.IgnoreCollision (col.gameObject.GetComponent<Collider>(),GetComponent<Collider>());
         }else if (col.gameObject.GetComponentInChildren<Collider>() !=null)
         {
             Physics.IgnoreCollision (col.gameObject.GetComponentInChildren<Collider>(),GetComponent<Collider>());
         }
     }
 }
 void OnCollisionExit (Collision col)
 {
     if (col.gameObject==player)
     {
         text.SetActive (false);
     }
 }
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 SterlingSoftworks · May 30, 2015 at 07:24 AM 0
Share

Try using Physics.IgnoreLayerCollision and set what NU$$anonymous$$BER layer you want to not collide.

avatar image kfchristian15 · May 31, 2015 at 02:58 AM 0
Share

Thanks for the suggestion, I tried it and changed the script to this:

public GameObject text; public GameObject player; int layer1; int layer2; Collider detector; bool ignore;

 void Start () {
     detector=GetComponent<Collider>();
     ignore=true;
     layer1=gameObject.layer;
 }

 void OnCollisionEnter (Collision col)
 {
     if (col.gameObject==player)
     {
         if (player.GetComponent<ThirdPersonCharacter>().hovering)
         {
             text.SetActive (true);
         }else{
             layer2=player.layer;
             Physics.IgnoreLayerCollision (layer1, layer2,ignore);
         }
     }else{ 
         layer2=col.gameObject.layer;
         Physics.IgnoreLayerCollision (layer1, layer2,ignore);
     }
 }
 void OnCollisionExit (Collision col)
 {
     if (col.gameObject==player)
     {
         text.SetActive (false);
     }
 }

What is weird is that there are two different gameObjects I don't want to collide and I gave them the same layer and one doesn't collide now and the other still does. I can work around it but I'd like to know why that is. Any ideas?

avatar image SterlingSoftworks · May 31, 2015 at 05:45 PM 0
Share

I am not sure why that's happening, HOWEVER!

First, with IgnoreLayerCollision if you go to where you assign the Layers of your scene they have numbers next to them.. You can use those as the layer numbers.

Second, you don't need to put the "ignore" at the end..

And third.. I should have said this in my above comment..

Try putting the Physics.IgnoreLayerCollision(##, ##) in the Start() function. This way it's set for good. Also, just using gameObject.layer inside it is fine, no need to set it to layer1. Also, if two colliding objects have the same layer but you don'y want them to collide, just put Physics.IgnoreLayerCollision(gameObject.layer, gameObject.layer)

Hope that clears up the collision issues.. If not, I am at a lose..

avatar image kfchristian15 · Jun 01, 2015 at 12:37 AM 0
Share

Thanks for the suggestions, it definitely shortened the code a bit. Here's the new one I made:

public GameObject text; public GameObject player;

 void OnCollisionEnter (Collision col)
 {
     if (col.gameObject==player)
     {
         if (player.GetComponent<ThirdPersonCharacter>().hovering)
         {
             text.SetActive (true);
         }else{
             Physics.IgnoreLayerCollision (11, 9);
         }
     }else{ 
         Physics.IgnoreLayerCollision (11,10);
     }
 }
 void OnCollisionExit (Collision col)
 {
     if (col.gameObject==player)
     {
         text.SetActive (false);
     }
 }

There are two weird things that are happening with it. One is what I mentioned before: there are two different gameOjbects both with layer 10 yet one of them still collides while the other does not. The second is that the player at first has their collision registered when flying and ignored when not, but this changes (I'm not sure what exactly makes it change but multiple times it worked correctly, and then did not). Thanks for all your advice SterlingSoftworks, it has definitely helped. Please let me know (anyone) if you have any ideas on how to fix this. Thanks!

0 Replies

· Add your reply
  • Sort: 

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

20 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

Related Questions

Surface with hole and Raycast - Which collider 1 Answer

Make Ray hit own collider 1 Answer

Ignore Collision problem 1 Answer

Trouble with Physics.IgnoreCollision 0 Answers

Mouse Movement with physics 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