Search Unity

Incorrect collision speed of an animated collider ?

Discussion in 'Scripting' started by graslany, May 29, 2013.

  1. graslany

    graslany

    Joined:
    Dec 26, 2012
    Posts:
    10
    Hello,

    I'm working on my first Unity project and I'm currently trying to make a GameObject - let's call it Attacker - hit another GameObject - let's call it Target - with an animated piston arm.

    $1xhc9M5.png

    Attacker has an animated mesh with two bones : a root bone, and an piston bone. A BoxCollider is attached to each bone, so that the piston can collide with Target when it is extended. When the piston extends, it pushes Target away. But once the piston is fully extended, Target has no speed ! It just stays where it was pushed instead of moving in the push direction.

    I tried to debug that by printing the contact points and relative speeds detected by the collisions between the piston and the target (the script is attached to the piston). I also made Attacker kinematic so that it does not rotate and I get a sequence of collision points alligned on the X axis. I get 4 collision points at each step, I guesse they are the 4 verticises of the collider face that hits the target. Here is the start of my debug log :

    Code (csharp):
    1. CollisionStay at speed 0.007714609
    2. Contact #0 between colliders PistonEnd_0 and Target at position (-20.6, 3.8, -2.3) and relative speed (0.0, 0.0, 0.0)
    3. Contact #1 between colliders PistonEnd_0 and Target at position (-20.6, 2.5, -2.3) and relative speed (0.0, 0.0, 0.0)
    4. Contact #2 between colliders PistonEnd_0 and Target at position (-20.6, 3.8, -1.0) and relative speed (0.0, 0.0, 0.0)
    5. Contact #3 between colliders PistonEnd_0 and Target at position (-20.6, 2.5, -1.0) and relative speed (0.0, 0.0, 0.0)
    6.  
    7. CollisionStay at speed 0.04774896
    8. Contact #0 between colliders PistonEnd_0 and Target at position (-19.8, 3.8, -2.3) and relative speed (0.0, 0.0, 0.0)
    9. Contact #1 between colliders PistonEnd_0 and Target at position (-19.8, 2.5, -2.3) and relative speed (0.0, 0.0, 0.0)
    10. Contact #2 between colliders PistonEnd_0 and Target at position (-19.8, 3.8, -1.0) and relative speed (0.0, 0.0, 0.0)
    11. Contact #3 between colliders PistonEnd_0 and Target at position (-19.8, 2.5, -1.0) and relative speed (0.0, 0.0, 0.0)
    12.  
    13. CollisionStay at speed 0.03424916
    14. Contact #0 between colliders PistonEnd_0 and Target at position (-19.0, 3.8, -2.3) and relative speed (0.0, 0.0, 0.0)
    15. Contact #1 between colliders PistonEnd_0 and Target at position (-19.0, 2.5, -2.3) and relative speed (0.0, 0.0, 0.0)
    16. Contact #2 between colliders PistonEnd_0 and Target at position (-19.0, 3.8, -1.0) and relative speed (0.0, 0.0, 0.0)
    17. Contact #3 between colliders PistonEnd_0 and Target at position (-19.0, 2.5, -1.0) and relative speed (0.0, 0.0, 0.0)
    18.  
    19. CollisionStay at speed 0.01031044
    20. Contact #0 between colliders PistonEnd_0 and Target at position (-18.0, 3.8, -2.3) and relative speed (0.0, 0.0, 0.0)
    21. Contact #1 between colliders PistonEnd_0 and Target at position (-18.0, 2.5, -2.3) and relative speed (0.0, 0.0, 0.0)
    22. Contact #2 between colliders PistonEnd_0 and Target at position (-18.0, 3.8, -1.0) and relative speed (0.0, 0.0, 0.0)
    23. Contact #3 between colliders PistonEnd_0 and Target at position (-18.0, 2.5, -1.0) and relative speed (0.0, 0.0, 0.0)
    As you can see, both objects have an indentical (null ?) speed according to the physics engline, although the piston end is moving ! I guess the engine is using the rigidbody's center for its computation (and this center is indeed not moving since the ship body stays a the same position). Target is displaced, but it is given no speed according to the engine.

    I think the speed computation should be based on the collider's speed, not the rigidbody's center speed ! Our current fix involves manually applying a force to Target in the collision detection callback, but this is a really, really dirty solution that raises other problems. Although I did not found any solutions to this problem on this forum or the Answers yet, I cannot believe there is no clean solution to such a common problem :-( - How would you fix that without reimplementing a physics simulation ?

    I thank you in advance for your answers.