Search Unity

Prevent two game objects from moving each other

Discussion in 'Physics' started by Verdemis, Sep 25, 2018.

Thread Status:
Not open for further replies.
  1. Verdemis

    Verdemis

    Joined:
    Apr 27, 2013
    Posts:
    92
    Hello everyone,

    I have the following situation - I have two game objects, both have a rigidbody attachted to them. Now I would like to find a way that those those two objects can't push each other around, but they should still collide, so that one object can't pass the other one.

    Is there a simple way to archieve this behaviour...?

    Best
    Verdemis
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    "can't push each other around"...."still collide"

    Those two statements seem like a contradiction. Collisions involve two objects overlapping, which causes the physics system to literally push them away from each other until they are no longer overlapping. So what you're trying to do sounds like it will be complicated because it seems like you're trying to defy the way physics works.

    Can you give some description of the general case you're trying to solve? My best guess about your situation is that maybe you're trying to have multiple players running around bumping into things, but where players can't push other players around? Let's say that was what you were doing, which sounds sort of feasible at first. It raises two kind of complex questions: 1) What if player A pushes some other rigidbody (a box) into player B? Would player B move? They're not directly being pushes by player A (they're being pushed by a box) so what should happen? and 2) Let's say Player A is pushed by some other force into Player B, perhaps by an explosion. Would Player B move in that case? Or would Player A just hitPlayer B and come to a dead stop?

    Your situation probably isn't what I'm describing, but I think you'll still run into issues with the two objects indirectly interacting, which would be complex. Anyway, maybe you can describe your specific situation, and other approaches might come to mind.
     
    PolyMad likes this.
  3. Verdemis

    Verdemis

    Joined:
    Apr 27, 2013
    Posts:
    92
    @dgoyette Thank you for your explanation. In my case I have two characters... which can move only on the x axis. They should not be able to walk past the other character, but they also should not push another around... but I still need physics for them.

    But at the moment it's seems like pushing each other around is not such a big problem as I thought at first. But knowing a way to prevent that would be nice.
     
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    Because of how specific your case is (single-axis movement), there's probably a reasonable workaround. Here's an approach that might work. I'm assuming you have some kind of Player Controller that accepts inputs and moves the player in the appropriate direction. What I'd do is detect when another player is immediately left or right of you (either via a raycast or via a trigger collider), and disable the player's left or right movement ability. In other words, if the other player is directly right of me, don't allow me to move right.

    This approach, however, would still allow normal physics forces to be transferred between players. For example, if Player A got pushed very fast into Player B, Player B would still move due to that collision. That may or may not be reasonable in your case.
     
    PolyMad likes this.
  5. PolyMad

    PolyMad

    Joined:
    Mar 19, 2009
    Posts:
    2,350
    You can use a couple of trigger zones around the players: when they meet, they can't go further.
     
  6. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    When they collide, modify the velocity to cancel against the direction toward the other object:
    • get direction from source to destination, normalized
    • rb.velocity = Vector3.ProjectOnPlane(rb.velocity, direction)
    When they collide, their velocity is removed in the direction of the other.
     
  7. GurkenSnuff

    GurkenSnuff

    Joined:
    Oct 11, 2020
    Posts:
    1
    Make a child on both objects give both the same collider as their parents have and a static rigidbody give the give them a different layer then the parents have use the collision matrix so the parents layers only collide with the children layers then make a script in which u use Physics or Physics2D.IgnoreCollision(Parent collider, child collider)
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    Please look at the dates of posts you're necroing. You're replying to a question from 2018.
     
Thread Status:
Not open for further replies.