Search Unity

Get OnCollisionEnter but then ignore bodies overlapping?

Discussion in 'Physics' started by matches81, Apr 19, 2019.

  1. matches81

    matches81

    Joined:
    Mar 29, 2014
    Posts:
    8
    Hi there,

    I'm trying to get VR melee combat to work. My basic weapon has some convex mesh colliders and a rigid body.
    The rigid body is switched to kinematic as soon as the player picks it up. So far, all is well. Hitting something also triggers the OnCollisionEnter method, as expected.

    However, my problems start after that first collision: Since I want the weapon to stick to the player's hand, there's nothing stopping the weapon from overlapping with the other rigid body, which causes the physics to go crazy trying to solve that overlap.

    My initial response was: Oh, that should be an easy fix. Just disable collision response between the weapon and the other body and it should be fine. If need be I can apply the appropriate impulse during OnCollisionEnter myself. Sadly, that doesn't seem to be that easy.

    Here's what I tried so far:
    1. Remove the rigid body entirely as soon as the weapon is picked up. Now I can move the weapon through other bodies just fine, but I don't get OnCollisionEnter calls.
    2. During OnCollisionEnter, use Physics.IgnoreCollisions with all my weapon's colliders and the collider I've struck. Undo this during OnCollisionExit. This doesn't seem to work at all. My assumption is that using IgnoreCollisions for the now overlapping colliders in question causes OnCollisionExit immediately / in the next frame which results in kind of a ping-pong state.

    To sum it up, here's my goal:
    1. Get OnCollisionEnter on my weapon to fire so I can react to that. OnTriggerEnter sadly doesn't provide contact points.
    2. Stop the weapon's rigidbody from causing the physics to go nuts during overlaps with other bodies.

    If anyone has any idea how to solve this, any help would be appreciated.
     
  2. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
    Use a joint.