Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Toggle isKinematic on Rigidbody re-trigger collision

Discussion in 'Scripting' started by mrCharli3, Aug 3, 2023.

  1. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    956
    Apparently since 2019 you cannot toggle isKinematic without re-triggering the collision. How am I now supposed to push back NavMesh Agents?

    It used to be you could disable agent, set kinematic to false, puch back, reset. Now I cannot do that anymore since when I set isKinematic to false, the collision is triggered again, calling the same logic...
     
  2. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    956
    Any ideas? This is such a silly problem to have in a game engine...
     
  3. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    I'm not sure what you mean, I've used:
    Code (CSharp):
    1. myRigidbody.isKinematic = false; // or true
    Many times recently. ?
     
  4. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    956
    It works fine, but if u toggle kinematic due to a collision, say knockback from collision with a sword, it will collide with that sword instantly again the frame you toggle kinematic, so it gets hit twice. This is the issue. This is "intended behaviour" but I have no clue how to do a simple knockback with a Navmesh agent now.

    i.e if you want to knockback an agent when it collides with a swords hitbox, you need to disable kinematic before you apply the force. And since you do that the same frame you get hit, it will re-trigger the collision.
     
  5. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Ohhh, I see now, sorry.

    I've dealt with that differently depending on the situation, there is Physics.IgnoreCollision() which you could temporarily set, while the sword is swinging around after said hit, BUT that ignores all collisions between layers(a,b). So that doesn't always work.

    Another time, I left it un-kinematic for a certain amount of frames, until the animation was over. But again doesn't work in some situations.

    And overall, I think I used raycasts, or left collisions to still trigger, but not hit(stay still or bounce from impact), and returned all collisions into a array. Then handled the array with if this layer, if that layer, etc... But that was for a wide swing type maneuver with a sword, hitting multiple enemies.

    But yeah, depends on the situation, and how you want to handle it(or how it messes things up later).
     
  6. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    956
    Thanks for some good ideas. I tried IgnoreCollision() but like you said I didnt work for this since I need it to be able to be hit again even if its being pushed back. Similar issue with second option.

    I just can see any scenario where this "intended behaviour" is a good thing, rly makes things complicated.

    My sword swing is in a wide arch so I have a large trigger that can hit multiple enemies. Not quite sure how to do it.
     
  7. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    There is a way to toggle it on and off.. for the life of me, I can't remember how..

    But yeah I found it's better to not have it hit, per se, within actions like these. Just still get the collisions, and only have it bounce when say dropping it on the floor or something. I'm thinking it had more to do with triggers, than full on collisions.

    And like I said with the collision array, that works perfect since you can save that enemy collider in a temp list(if collider not in temp list), so that it doesn't get hit 2,5,10 times(depending). And just Clear() said temp list once animation is over, or swing method.