- Home /
Disable collision between mesh collider and character controller
So I have a door and the standard fps controller and I can open the door, slerping it 90deg. I want to disable collision between the door and the player so that if the player is in the way when the door is rotating, the door goes through the player. The door has a mesh collider and a capsule collider trigger. I set a bool var when the door is activated and unset it if the door has reached a close angle/open angle.
So far, I've tried using physics.ignorecollision(), disabling the door's mesh collider and even removing the collider component entirely when the bool var is set but nothing seems to work. I'm assuming the problem is in the way I'm rotating the door i.e. changing localRotation. Any help please?
Answer by mouser013 · Jul 15, 2015 at 02:53 PM
Okay, I figured it out. I forgot that the door object had child meshes with colliders and changing the layer of the parent doesn't affect the children. I updated the script to go through the children and change their layers and now it works fine.
Glad to hear it is finally working! I think that you should mark as answer the one below where all the conversation went on for clarity tho.
Answer by Aridez · Jul 14, 2015 at 01:05 AM
In unity menu at the top, on Edit>Project settings>Physics you can find something called layer collision matrix. To avoid collisions create a layer called "Player" for your player and another called "Doors" (for example) and put all the doors in this second layer, then go to the layer collision matrix and uncheck the Player-Doors checkbox.
I want to ignore collision only when a bool is set, not all the time.
What's exactly the situation? I guess that you want the collision only when the door is closed, in that case, while the doors are closed you can put them on a "Wall" layer (or some other special layer if you want some special interactions with closed doors) and then when opening you could change the layer to "Door" or "OpenedDoor" or something like that. While closed it would act as a wall and while opening the collisions would be ignored.
I don't want collision while the door is moving. I tried changing the layer before starting to rotate the door and changing it back after but that didn't work either.
I found some code that might be useful for you here, changing the layers as I said should be working so maybe something is wrong in your code. Try this: (I took the code from the other question)
if (ghost$$anonymous$$ode){
gameObject.layer = 8; // move to ghost layer
} else {
gameObject.layer = 0; // move to regular layer
}
Adapt it to your case and if you still have problems let me know and we'll try to see what's going wrong, good luck!
Changing to the new layer works if I do it beforehand. But it doesn't work if I do it ingame.
Here's the relevant part:
void Update()
{
if (active && Input.GetButtonDown ("Interact"))
{
if(!isLocked)
{
if(isOpen)
{
gameObject.layer = 9;
isOpen = false;
}
else
{
gameObject.layer = 9;
isOpen = true;
}
complete = false;
}
else
{
StartCoroutine (InfoText ());
}
}
if(!complete)
{
if(isOpen)
{
transform.localRotation = Quaternion.Slerp (transform.localRotation,openAngle,Time.deltaTime * smoothing);
}
else
{
transform.localRotation = Quaternion.Slerp (transform.localRotation,closeAngle,Time.deltaTime * smoothing);
}
if ((isOpen && Quaternion.Angle (transform.localRotation,openAngle) < 1 ) || (!isOpen && Quaternion.Angle(transform.localRotation,closeAngle) < 1))
{
gameObject.layer = 0;
complete = true;
}
}
}
The idea is to ignore collision while complete is false.
EDIT: I just tried changing to the new layer in an Awake() and it turns out it had no effect. The layer had changed in the inspector but the player would still collide with the door.
FURTHER EDIT: Disregard. I figured it out. Thanks!
Answer by zedseven · Jul 15, 2015 at 03:46 PM
Why don't you just disable the door's mesh collider?
door.GetComponent<MeshCollider>().enabled = false;
~zedseven
I still need the collider for other stuff. Anyway I've figured out what went wrong.