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 Rigidbody 2D - Kinematic bodies not colliding between them

Discussion in '2D' started by Altia, Dec 4, 2020.

  1. Altia

    Altia

    Joined:
    Nov 14, 2014
    Posts:
    13
    Hi!

    I am having an issue with Rigidbody2D using Kinematic body type.
    I have let's say a zelda like game with a main character (MC) having a dynamic rigidbody2D, and enemies having a Kinematic rigidbody2D. This is done this way so that enemies are not moved by the physic engine when colliding with the MC.
    The thing is that, since Unity 2019 (I think), there is an option called "Use Full Kinematic Contact" that allows Kinematic bodies to collide together (which was not the case before). Link to the documentation: https://docs.unity3d.com/Manual/class-Rigidbody2D.html#UseFullKinematicContactsProperty

    But when I activate this on my different enemies, they still go through the other enemies, not colliding.
    Any explanation about this? Is there something else to do than just enabling the "Use Full Kinematic Contact" option?

    Thanks!
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    This option produces contacts for Kinematic vs kinematic/static (which by default do not contact) so with it on you'll get callbacks indicating it has collided. If it were to produce a collision response too then it'd no longer be a Kinematic body but a Dynamic one and you'd just use that.
     
    Altia likes this.
  3. Altia

    Altia

    Joined:
    Nov 14, 2014
    Posts:
    13
    Hey MelvMay!
    Thanks for your answer. So I guess I need to manage the collision and stop the movement in the callbacks right?
    I switched to Kinematic because I wanted to get rid of the physical collision happening with Dynamic bodies which are unwanted in a zelda like game (typically enemies were bounced away from the main character as soon as it entered in collision with). Am I doing it wrong? Should I keep using Dynamic bodies but with different paremeters to avoid this bounce effect?

    One more time, many thanks for your answer, appreciate it :)
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    I just don't follow what you want TBH. You don't want a collision response but you do? :)

    You seem to want it to "stop" but also not overlap or move so I think you need to be clear what you actually want.

    With FullKinematicContacts on you'll get callbacks and contacts you can retrieve so you can do whatever you like i.e. set velocity, position etc.
     
  5. Altia

    Altia

    Joined:
    Nov 14, 2014
    Posts:
    13
    Hehehe hard to explain. I do want a collision to happen, but I do not want any bounce effect, etc. I just want the two colliders to enter in contact and stop any movement from both colliders (so that no one is pushed one way or the other). Basically, a character entering in collision with an enemy should be equivalent to pushing a rock that you could not move at all because it is too heavy. And I would like to have the same behavior between the enemies themselves (so I tried moving Mass/Linear Drag/Angular Drag, but of course if I have enemies of the same type, they have the same parameters and can push the others...)
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    You see, this isn't clear still.

    Collisions are not directional. An enemy doesn't collide with a character and then a character collide with an enemy; it's one thing. Any collision is a slight overlap, sometimes it can be big. So stop them overlapping requires one or both colliders (specifically the bodies they are attached to) to move. You don't want one to move the other so how can they resolve the overlap? Which can move and when?

    In the end, you are free to see the collision then resolve the overlap yourself. For instance if both were Kinematic using full-kinematic-contacts then when you saw the collision, you can resolve the overlap yourself. Physics2D.Distance (also Collider2D/Rigidbody2D.Distance) will calculate the separation/overlap which you can use to position one or both of your choice to move out of overlap.

    To stop overlap in the first place (which is preferable) then use the Rigidbody2D.Cast and Collider2D.Cast methods to determine how far you can move. I provide a demonstration of pure Kinematic movement here showing how casting can be used to determine how a Kinematic body can move (link to the repo is in the description):


    The main repo which demonstrates a whole bunch of 2D physics is here: https://github.com/Unity-Technologies/PhysicsExamples2D
     
    Altia likes this.
  7. Altia

    Altia

    Joined:
    Nov 14, 2014
    Posts:
    13
    Thanks for the example and the repo, it might help make a choice in my case.
    I'll try to explain one more time, and sorry if it is not fully clear, but please understand I'm neither a unity pro, nor a native english speaker.
    When using Dynamic rigidbody, when a collision happens between two colliders of a same Mass/etc., one of the body is then pushed back indifinetly, drifting for even in a direction. This is the kind of physic calculation and force applied to the object I do not want. What I want is that when a collision happens, only a "re-alignment" is done between the two objects, meaning a very small movement. As in your video example where the character is slightly realined when entering in collision with the wall, giving the feeling that he just sticks to the wall. He is not sent miles and miles away as it is with Dynamic Rigid Bodies.
     
  8. Altia

    Altia

    Joined:
    Nov 14, 2014
    Posts:
    13
    Here is a video of what happens with Dynamic bodies and that I don't want to happen. I just would like the yellow slime to stay in place and stop the character movement.
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    You're not using any linear drag so any velocity from the collision force won't be damped so it'll continue moving. If you don't want the dynamic behaviour then don't use dynamic bodies and use kinematic ones instead with full-kinematic -contacts as I went over above.

    It's still hard to give you an answer because you're showing a character pushing into an enemy continuously and seem to want the enemy stay in place and not move. To do that your character can be dynamic but the enemy needs to be static (if never moving) or kinematic (if it moves) but then you talk about the other way around so it's hard to know how this enemy moves.

    If you want full control then make them kinematic as I described above. Kinematic don't react to forces such as gravity and therefore do not have collision forces on themselves.
     
  10. fullace

    fullace

    Joined:
    Aug 18, 2016
    Posts:
    2
    I leave a solution for someone who are finding a way.