Search Unity

How to have Rigidbody act as a trigger only when colliding with Character Controller?

Discussion in 'Physics' started by dkstrong, Jul 7, 2018.

  1. dkstrong

    dkstrong

    Joined:
    Aug 17, 2013
    Posts:
    16
    I am making a "pickup" item that I want to act in the physics world. E.g. respond to impulses and explosions, bounce around on the ground, etc. However players (CharacterController) can pick these items up.

    If I give the object a "Trigger" collider the character controller can walk over and seamlessly pick it up.

    However when gravity acts upon it it will fall through the floor.

    If I un check the IsTrigger and treat it as a normal collision. Then I have an issue when the character picks the item up, it creates a small stutter in the player's movement as the character is stopped by the rigid body for a frame or two before the item is picked up/destroyed..

    How can I treat the item as a trigger when interacting with a Character Controller, but otherwise be a normal non trigger Rigidbody?
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    I'd approach this by having a parent object and a child object on two different layers. For each Layer, you can choose which other layers the object collides/interacts with.

    In this case, the parent object would on on a layer named "CollidesWithEverythingButThePlayer", and it would contain the rigidbody. The child object would be an identical collider, but on a "CollidesOnlyWithThePlayer" layer, and it would be a Trigger collider. (Call the actual layer names what you want, but set up their collision settings in the Physics settings.)

    The result will be an object that interacts realistically with everything except the player. The player won't interact with the parent object at all. Instead, they'll interact with the trigger object only.

    Another way is to forget that complexity and just use OnCollisionEnter to trigger your player pickup. There are some downsides (it might slow the player down to run into it, even if you destroy it on the frame the player collects it, and it could hit your player like a cannon ball if it's moving fast enough), so it's up to you.