Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Preventing Rigidbody characters from pushing eachother

Discussion in 'Scripting' started by Fluorite, Sep 3, 2012.

  1. Fluorite

    Fluorite

    Joined:
    Jul 11, 2012
    Posts:
    4
    I'm building a game which consists of a lot of characters which can push objects around and be pushed around by objects in the environment (and forces applied via code), they all consist of a non kinematic rigidbody and collider and it works fine for the environmental collisions, however i don't want the characters to be able to push eachother i want them to treat eachother like two character controllers colliding.

    So my question basically, is there a simple way of telling the physics engine not to apply force to a character when another character runs into it, or alternatively a way to apply a counter force so that both units hold their ground?
     
  2. Fluorite

    Fluorite

    Joined:
    Jul 11, 2012
    Posts:
    4
    Solved nevermind :) just doing a sweeptest eachframe now and applying an equal opposite velocity if a collision is going to occur between characters (hopefully this doesn't have a performance knock on)
     
  3. RazorCut

    RazorCut

    Joined:
    May 7, 2009
    Posts:
    393
    Wouldn't putting the characters on a layer and have that layer ignore collisions with itself work? There is a collision matrix in the physics settings inspector to do that latter part. I suggest this because I have done this in a game I'm working on now; the "enemies" are all specified like this so that they cannot collide with each other but collide with the environment just fine.
     
  4. Fluorite

    Fluorite

    Joined:
    Jul 11, 2012
    Posts:
    4
    I need the characters to still collide so they don't pass through each other, just didn't want them being able to push each other around since they are all rigidbodies
     
  5. timsk

    timsk

    Joined:
    May 27, 2011
    Posts:
    177
    Sweeptesting is a pretty expensive operation, especially once you throw it in update.

    An easy fix for this would be to set the rigidbodies to kinematic when they collide. Use tags to check if the object you are colliding with is another character. Just remember to set kinematic to false again once they aren't colliding.

    OnCollisionEnter

    OnCollisionExit

    This is a pretty standard procedure when working with rigidbodies, I've used it loads and I see it used a lot in other projects.