Search Unity

How can I make capsules rigid bodies pass through each other?

Discussion in 'Scripting' started by From-Soy-Sauce, Jul 24, 2014.

  1. From-Soy-Sauce

    From-Soy-Sauce

    Joined:
    Jan 7, 2014
    Posts:
    162
    Hi in my game I've got capsules for characters and cubes for the world. I like how the collision with the world works, it's nice. But I was wondering how I would go about to make it so that the capsules can pass through each other.
     
  2. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
  3. From-Soy-Sauce

    From-Soy-Sauce

    Joined:
    Jan 7, 2014
    Posts:
    162
    IgnoreLayerCollision sounds like it is what I am after. Would you explain to me how I'm supposed to implement the code please? I've tried copying and pasting it and a few other tinkering around, I can't seem to figure it out.
     
  4. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    Sure, so to use IgnoreLayerCollision we need to set which layers to ignore, you could just pass in the index of the layer in the layer array but we could also just use LayerMask.value to make it nice and easy to use in the editor.

    Something like this:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class ColliderIgnore : MonoBehaviour {
    6.  
    7.     public LayerMask layer1; //The first layer you want to ignore collision with
    8.     public LayerMask layer2; //The  second layer you want to ignore collision with
    9.  
    10.     void Start () {
    11.         Physics.IgnoreLayerCollision(layer1.value, layer2.value, true); //Now it'll ignore collision between all objects on layer one and all objects on layer 2
    12.     }
    13. }
    14.  
     
  5. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    Alternitavely if you just want to ignore collision between the two objects instead of all objects on the layer then do something like this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ColliderIgnore : MonoBehaviour {
    5.  
    6.     public GameObject GO1; //The first GameObject you want to ignore collision with
    7.     public GameObject GO2; //The second GameObject you want to ignore collision with
    8.  
    9.     void Start () {
    10.         Physics.IgnoreCollision(GO1.collider, GO2.collider); //Now it'll just ignore collision between two objects instead of all the objects on the layers
    11.     }
    12. }
    13.  
     
  6. From-Soy-Sauce

    From-Soy-Sauce

    Joined:
    Jan 7, 2014
    Posts:
    162
    Sorry I'm kinda slow, it sorta works but not in the way I want it to (now the pills can only collide with each other):
    Physics.IgnoreLayerCollision

    For some specifics, I'd like to ignore layer 8(named "CharLayer") .

    What were you saying about just putting the index in there? I'd like to know more about that.
     
    Last edited: Jul 24, 2014
  7. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    Physics.IgnoreLayer takes two integers for the layers, look at the inspector for the script with layer masks. You should be able to select your CharLayer.. If you don't want to do it that way then just set the layer yourself like:

    Code (CSharp):
    1. Physics.IgnoreLayerCollision(8, other layer integer value, true);
     
  8. From-Soy-Sauce

    From-Soy-Sauce

    Joined:
    Jan 7, 2014
    Posts:
    162
    -edit-

    THANKS!
     
  9. From-Soy-Sauce

    From-Soy-Sauce

    Joined:
    Jan 7, 2014
    Posts:
    162
    Though I have another question. While they don't collide with each other, they still seem to be able to hit each other Physics.Raycast()

    Is there anyway I can make them ignore that too?
     
  10. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
  11. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  12. From-Soy-Sauce

    From-Soy-Sauce

    Joined:
    Jan 7, 2014
    Posts:
    162
    Thanks! it worked!
     
  13. Pysassin

    Pysassin

    Joined:
    Dec 27, 2012
    Posts:
    87
    If you want one layer to ALWAYS avoid another layer I find it much easier to just change your projects Physic settings where it has a nice little boolean graph of what layers a certain layer should interact with.