- Home /
Rigidbody Ignore another Rigidbody?
Hello! So I have an interesting questions- Is it possible and how to make it so 2 rigidbodys Ignore each others physics?
Basically I have rigidbody A and rigidbody B and I want to make it so that they don't affect each others position. I still want them to collide with each other but I don't want them to be moved by one another. Yes, I could change the mass of rigidbody A and make it higher but then rigidbody A wouldn't be affected by rigidbody B but Rigidbody B would still be affected by rigidbody A.
Anyway long story short- How to make 2 rigidbodies ignore each others rigidbodies but still register each others colliders like when a rigidbody hits a rigidbody it acts as the other rigidbody is a collider instead of a rigidbody?
I have been all over the internet and haven't found a solution. I have no idea how to make this work.
Answer by DocMcShot · Mar 09, 2015 at 08:47 PM
Yes. You should be able to write 2 scripts and attach one to each of the objects. It really depends on what you want to happen if the 2 objects do collide. Do you want them to just immediately stop moving? Do you want the objects to be able to pass through each other? What are you wanting to do? If you want them to immediately stop you could do something like this:
Object 1 Script:
void OnCollisionEnter(Collision other)
{
if(other.gameobject.name == "myFirstGameobject")
{
gameObject.rigidbody.velocity = Velocity.zero;
gameObject.rigidbody.angularVelocity = Velocity.zero;
// You may also want to use the function:
// gameObject.rigidbody.Sleep();
}
then on your second GameObject attach a script like:
void OnCollisionEnter(Collision other)
{
if(other.gameobject.name == "mySecondGameobject")
{
gameObject.rigidbody.velocity = Velocity.Zero;
gameObject.rigidbody.angularVelocity = Velocity.Zero;
// You may also want to use the function:
// gameObject.rigidbody.Sleep();
}
Another good solution (depending on what you are trying to do) may be using the Layer Based Collision. See the Unity Manual to use it. Here is the link to it. http://docs.unity3d.com/Manual/LayerBasedCollision.html
Basically I just need them to collide with each other but not move each other. Like if Rigidbody A flies into Rigidbody B, I don't want Rigidbody B to move, what I need is Rigidbody A to act like Rigidbody B is a collider ins$$anonymous$$d of rigidbody and same when Rigidbody B flies into Rigidbody A.
So it's like LayerdBasedCollision but ins$$anonymous$$d of ignoring collisions I need them to ignore rigidbodies.
Also I tried the scripts but they didn't seem to work, they had few errors that I fixed but they didn't seem to do anything.
Now I actually did find some kind of a solution that involves LayerBasedCollision. It's a little "sloppy" but it seems to work more or less. Basically both rigidbodies have a child object that has $$anonymous$$inematic rigidbody attached to them and has a collider that's slightly bigger radius than the parent collider and I made it so Parent and Child colliders ignore each other.
So long story short- now both Objects carry around extra colliders that stops their rigidbodies from colliding, so it's like a wall between 2 rigidbodies atm to stop them from affecting each other. But like I said it's a little sloppy atm and if possible I would like to see someone come up with better solution than this.
Answer by alok-kr-029 · Mar 09, 2015 at 09:09 AM
you can check the istrigger component in its collider in inspector by this you can check if they trigger each other and they will not effect its position
Ok tried it but what it seemed to do is ignore every collision in the scene which is exactly opposite what I need. I need it to still collide with everything in the scene.
Answer by wahaca · Jan 16, 2017 at 03:05 PM
Maybe put them in two different layers and use Layer Based Collision Detection: https://docs.unity3d.com/Manual/LayerBasedCollision.html
Answer by Alverik · Mar 28, 2020 at 07:45 AM
Physics.IgnoreCollision(collider 1, collider 2, true);
or false if you want to revert it.
Answer by bisdaeas · Mar 28, 2020 at 09:16 AM
You can use unity's built in matrix system that allows you to chose what layers can interact with others.Official Site