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

Rigidbody 2D won't collide with another object when it's moving really fast

Discussion in '2D' started by Lidanh, May 29, 2014.

  1. Lidanh

    Lidanh

    Joined:
    Mar 27, 2013
    Posts:
    29
    Hi,

    I have 2 objects:

    object A:
    A really fast moving object that has rigidbody 2D and a Trigger Circle Collider.
    object A is moving on the screen using transform.position.

    object B:
    A slow moving object that its parent has a rigidbody 2D (it moves wherever its parent is going) and has a Trigger Box Collider.
    object B's parent is moving on the screen using gravity.

    here is the story:
    The function OnTriggerEnter2D on object A is being called when object A is moving slow. If it moves really fast, the OnTriggerEnter2D is not being called every time.

    Now, I read over this page: http://docs.unity3d.com/Manual/class-Rigidbody2D.html
    That there is something called Continuous Collision Detection. It looked like the solution to my problem.

    Unfortunately changing Object A's Collision Detection from Discrete to Continuous did not solve the issue.

    I couldn't find any more documentation regarding the rigidbody 2D Continuous Collision Detection. I must be doing something wrong.

    Please let me know if you have any thoughts.

    Thanks,
    Lidan
     
    Last edited: May 29, 2014
  2. jamesar

    jamesar

    Joined:
    May 21, 2013
    Posts:
    9
    +1

    Also having this problem after upgrading to Unity 4.5. Basically, I have 2d objects that contain a RigidBody2D, and a CircleCollider2D that fall from the top of the scene and collide with my player at the bottom of the scene. Some of the objects will register in the OnCollisionEnter2D function of the player class while others do not. I tested the theory further by rolling back to Unity 4.3.4 and everything was working properly. Am desperately waiting for a solution to this problem, otherwise I will probably end up rolling back to 4.3.4 again to finish the game.

    Thanks for the help,
    James
     
    Last edited: May 30, 2014
  3. Warped-Imagination

    Warped-Imagination

    Joined:
    May 18, 2014
    Posts:
    51
    Have you tried using transform.Translate instead of setting the position directly?
     
  4. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    When a rigidbody move at high speeds, it is possible to completely skip over a collider. Eg: Physics frame 1 the rigidbody is completely on the right of the collider. Physics frame 2, the rigidbody is completely on the left side of the collider.

    Since the colliders never overlap, no collision is detected. Continuous mode helps with this sometimes, but I haven't seen it work 100% consistently.

    If you have a Rigidbody2D, you should be moving it in FixedUpdate() using rigidbody2D.MovePosition(Vector2). This will move the Rigidbody2D through space, instead of teleporting it from point A to point B. There is a limit to this however.

    To really do this properly, you should RayCast (or similar) every frame to make sure there are no colliders between point A and point B. This way, even if the rigidbody is going very fast, the RayCast will tell you if an object is in the way.

    This is an example script for 3D physics, though it is probably overkill for your needs.
    http://wiki.unity3d.com/index.php?title=DontGoThroughThings
     
  5. Lidanh

    Lidanh

    Joined:
    Mar 27, 2013
    Posts:
    29
    Hey guys,

    Thank you so much for your answers.
    I have tried really everything you suggested.
    Only one thing worked! I have converted the RayCast code that Garth suggested to 2D and it worked really well.

    Thanks again :)
    Lidan Hackmon
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,417
    Continuous collision detection in Box2D should work flawlessly if you're not repositioning the rigid-body yourself. There's stress-tests for this in the Box2D test-bed which highlights how robust this is. There should be no need to perform ray-casts or some other workaround.
     
  7. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    In this case, however...
     
  8. Lidanh

    Lidanh

    Joined:
    Mar 27, 2013
    Posts:
    29
    Hi,

    Thanks for your input.
    I'm using transform.position because I have a fixed position I want this object to do to (comes from Touch input).
    If you know any other way I can move this rigidbody without harming the Continuous collision detection feature, please let me know.

    Thanks,
    Lidan
     
  9. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    Rigidbody2D.movePosition is the appropriate function for directly moving a rigidbody while still colliding. However, you'll have to calculate your offset from your desired target position.
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,417
    Yes, as was mentioned, Rigidbody2D.MovePosition (and Rigidbody2D.MoveRotation) as the way to go here. These calculate the velocity required to move the object to the specified position/angle in the next physics update. This means the object moves under the physics systems control and does not simply warp from one position to another (bypassing collision detection between those two points).

    This is a new feature for 4.5. Box2D previously capped the maximum velocity as a compile-time constant but I changed that so it can be changed at runtime. I also set it to be quite large so that movePosition/Rotation works for most cases. You can find this setting in the Physics2D settings should you ever wish to change it.
     
  11. BrainAndBrain

    BrainAndBrain

    Joined:
    Nov 27, 2014
    Posts:
    115
    This series of posts was extremely helpful to me. I'm moving a 2D character via transform.position, but still wanted to be able to keep the character from going certain places by placing colliders. Rigidbody2D.MovePosition nicely allows that without forcing me to move the character via velocity. Thanks!