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.
There are many ways of doing it depending on what your needs are; Physics.IgnoreCollision may be what you’re looking for: Unity - Scripting API: Physics.IgnoreCollision
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.
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:
using UnityEngine;
using System.Collections;
public class ColliderIgnore : MonoBehaviour {
public LayerMask layer1; //The first layer you want to ignore collision with
public LayerMask layer2; //The second layer you want to ignore collision with
void Start () {
Physics.IgnoreLayerCollision(layer1.value, layer2.value, true); //Now it'll ignore collision between all objects on layer one and all objects on layer 2
}
}
Alternitavely if you just want to ignore collision between the two objects instead of all objects on the layer then do something like this:
using UnityEngine;
using System.Collections;
public class ColliderIgnore : MonoBehaviour {
public GameObject GO1; //The first GameObject you want to ignore collision with
public GameObject GO2; //The second GameObject you want to ignore collision with
void Start () {
Physics.IgnoreCollision(GO1.collider, GO2.collider); //Now it'll just ignore collision between two objects instead of all the objects on the layers
}
}
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.
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:
Physics.IgnoreLayerCollision(8, other layer integer value, true);
-edit-
THANKS!
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?
If you look at the docs for ray cast: Unity - Scripting API: Physics.Raycast it has the option to ignore a layer mask, so you just need to input the layer mask to ignore when ray casting.
there’s also: Unity - Manual: Layer-based collision detection
but that would affect everything assigned to the layers in question
Thanks! it worked!
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.