Search Unity

How to disable push force between two rigidbodies?

Discussion in 'Physics' started by omermecitoglu, Aug 1, 2017.

  1. omermecitoglu

    omermecitoglu

    Joined:
    Jul 24, 2017
    Posts:
    5
    Hi, I'm working on a game project. Character objects have rigidbody + capsule collider. They move with velocity, but I don't want them to push each other. Just like in this video



    the ways I tried so far:
    • giving ".isKinematic = true" to objects when they collide (OnCollisionEnter)
    • changed enemy characters' rigidbody mass. with this way player can't push its enemies but enemies can :/

    Is this possible with Unity?
     
  2. omermecitoglu

    omermecitoglu

    Joined:
    Jul 24, 2017
    Posts:
    5
  3. Leniaal

    Leniaal

    Joined:
    Nov 7, 2012
    Posts:
    119
    @omermecitoglu Can u explain how this solved it for you? Wouldn't ignore collision just make them pass right through each other as if they were non-existent?
     
    CheekySparrow78 likes this.
  4. CamargoNL

    CamargoNL

    Joined:
    Mar 30, 2020
    Posts:
    1
    Sorry to necropost, but after fiddling with this problem for a good 3 hours I figured out what the other thread meant and it works perfectly. So what I did was I had a parent object with a non-trigger collider and a dynamic rigidbody. I then added a child to this object with a non-triggercollider that is slightly larger than the parent collider and added a kinetic rigidbody to the child object. In order for the parent and child non-trigger colliders to no collide, you set IgnoreCollision(parentcollider, childcollider) in a start method.

    The behavior of multiple objects with this setup is that they no longer push eachother, since when one object tries to push the other, he will enter the kinetic collider of the other with his own dynamic collider, which it cant push and vice versa. I hope this clears things up for you.
     
    martyparty1535, Baloouis and samathon like this.
  5. Leniaal

    Leniaal

    Joined:
    Nov 7, 2012
    Posts:
    119
    Hi @matsvanderheide ,

    Thanks for sharing. Yeah we solved it exactly like this back then, but never shared it here. But good you did for other people that stumble across this in the future. It's actually super simple, but it took me a good time to figure it out. Our exact solution (maybe it's helpful for others):

    First collider:
    upload_2020-3-30_17-20-19.png

    Second collider (child object of Player): Only let this collider collide with objects of layer Player
    upload_2020-3-30_17-20-48.png

    To prevent it from colliding with it's own playerblocker:

    Code (CSharp):
    1. Physics.IgnoreCollision(PlayerCollider, PlayerBlockerCollider, true);
     
  6. timsamoff

    timsamoff

    Joined:
    Sep 9, 2017
    Posts:
    5
    My solution was similar to the @Leniaal's, although:
    1. I didn't find it necessary to add the script – there wasn't an issue with the two colliders colliding in my case.
    2. I set the parent collider (the RigidBody) Layer to not collide with other RB colliders and the child collider Layer to collide with other RBs.
    3. I set the Mass of the other RBs higher (and played with the Drag and AngularDrag), although, honestly, some pushing felt good.
    Not the most elegant solution, but it works.
     
  7. SIWWC

    SIWWC

    Joined:
    Oct 31, 2017
    Posts:
    13
    Wow, Thanks your reply, I find the resolution for a long time, the method is very good!
     
  8. Tutoroot

    Tutoroot

    Joined:
    Feb 1, 2023
    Posts:
    7
    Thanks for your reply it is very informative