- Home /
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);
}
}
Try using Physics.IgnoreLayerCollision and set what NU$$anonymous$$BER layer you want to not collide.
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?
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..
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!
Your answer
Follow this Question
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