Search Unity

2D kinematic rigidbody collision detection

Discussion in 'Physics' started by aElSi2, Jul 8, 2020.

  1. aElSi2

    aElSi2

    Joined:
    Feb 20, 2019
    Posts:
    12
    I'm trying to write a 2d character controller without using dynamic rigidbodies because they need messy workarounds like setting physics materials, disabling gravity, terminating forces, etc.
    The best method for doing that i found so far is using kinematic rigidbodies with full kinematic contacts enabled.
    Here is the problem: I don't know how to stop my rigidbody on contact. I can get the contact info but there is no such thing like raycasthit2d.centroid. How do i calculate the place where rb2d should stop?
     
  2. aElSi2

    aElSi2

    Joined:
    Feb 20, 2019
    Posts:
    12
    Rb2D.Cast doesn't work for me because it can't have more than one hit point per collider.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    If you look at my signature you'll see a GitHub repository that contains lots of examples of 2D physics features. One miscellaneous example might be useful here as it shows you full kinematic movement. In the example it does stuff like sliding across surfaces which you might not need but it does show you clearly kinematic movement.

    KinematicTopDownController

    Just download the repo and load-up the scene of the same name as the script above.

    Hope it helps.

    EDIT: You shouldn't need to be interested in the centroid, you should be able to use distance of the cast to tell you how far you can move.
     
    aElSi2 likes this.
  4. aElSi2

    aElSi2

    Joined:
    Feb 20, 2019
    Posts:
    12
    Yes! Thank you! This is exactly what i wanted to achieve! I found your physics2d example project super useful.