Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

[SOLVED] Rigidbody also for child colliders?

Discussion in 'Physics' started by Tetsubo, Jan 15, 2017.

  1. Tetsubo

    Tetsubo

    Joined:
    Jun 26, 2014
    Posts:
    40
    Hello Unity Community,

    for my Unity 5 2D game I have a character, which consists of multiple components (sprites). For each hand it has separate collider (as trigger). On my parent object of the character I have a rigidbody2d + another large collider (trigger) to detect there is a "danger" near.

    As far as I understand (after couple of hours reading through forum threads), I understood that for a collider to work as it should and to not loose performance, a rigidbody should be also attached to it. What I didn't found anywhere is: does that include also child objects?? Do I have to attach also rigidbodies to the hands of my character for colliders to work consistently without performance lost?

    Thanks for any response!
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    2,049
    You don't need to add a Rigidbody2D for 'peformance reasons', you add it to indicate how/when/if the colliders move.

    Without a Rigidbody2D a Collider2D is implicitly Static i.e. you're saying you don't want it to move and it's added to a hidden static Rigidbody2D that lives at the world origin. Assuming you're using 5.5, adding a Rigidbody2D allows you to select which body type you want (Static, Kinematic or Dynamic).

    If you're using a Unity version before 5.5 then the bodyType property isn't available and to use Static, you only have the option of not adding a Rigidbody2D. There's a IsKinematic property that allows you to toggle between Kinematic or Dynamic.

    If you want the move the Rigidbody2D yourself then add a Rigidbody2D set to be Kinematic. This won't allow forces to be applied and will move child bodies if parent bodies are moved.

    Try not to set the Transform position/angle (you should never do this when you have a Rigidbody2D attached as it's in control of the Transform) but instead use Rigidbody2D.MovePosition and Rigidbody2D.MoveRotation.
     
  3. Tetsubo

    Tetsubo

    Joined:
    Jun 26, 2014
    Posts:
    40
    Thanks for the answer @MelvMay !

    .. and to be honest, I expected a different answer, because of something I found a couple of minutes ago: https://docs.unity3d.com/Manual/class-Rigidbody.html

    About Compound Collider it's said, that colliders on ChildObjects without rigidbody(rigidbody is on the parent's GO) is the way to go. Am I missing something? I am using Unity 5.5.

    Also, the child GOs are not being moved - just the parent object. So, in that case, they are not moving for themselves, but the parent GO (and it's rigidbody) "move them", as the parent object is moving. How to categorize the child colliders and rbs - static, kinematic? Also if I use animation to move the transform of one of the child game object (let's say the hand is moving backwards/forewards), does that mean they are "moving" and I have to attach a rigidbody and set it to kinematic(?) ?

    Even more confusing for me - on an enemy (single KINEMATIC rigidbody with a single collider), with a script I am setting in Start() it's velocity (rb2d.velocity) and it moves (gravity and other forces are not applied). As far as I understood, for kinematic RBs the movement should be done ONLY with rb.MovePosition() and setting velocity directly "does not work".. What I am missing/misunderstanding?
     
    llMarty likes this.
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    2,049
    Note that you're referring to the 3D rigibody and not the 2D one!

    Either way, colliders attach to a Rigidbody(2D) on the same GameObject or one on a parent GameObject. This has always been like this so ignore any version.

    You really shouldn't nest Rigidbody(2D) unless they're Kinematic. Kinematic bodies are under your control and won't be moved by the Physics system due to collisions (forces).

    With Rigidbody2D you can set the linear/angular velocity explicitly if you want. You cannot however apply forces to adjust the Rigidbody2D nor does gravity (which is just a force) work on it.

    Sounds like you're trying to do a lot of stuff in one go without understand the basics. A few simpler tests will help you.
     
  5. Tetsubo

    Tetsubo

    Joined:
    Jun 26, 2014
    Posts:
    40
    @MelvMay , sorry, didn't know rb3d and rb2d have different stategy for collision detection. What I am trying to understand, is what the engine expects and does internally, so I can set up my objects accordingly. In my example:

    Character (has 1 x dynamic rb2d, 1 x collider to detect collisions with walls ONLY)
    --- torso (has attached: 1x sprite, 1 x collider as trigger, which detects bullets that "hit" it, 1x script which handles OnTriggerEnter2d(bullet) ). Not animated, not being moved by a separate script etc.
    --- left hand (has attached: 1x spirte, 1x collider as trigger, which detect bullets that "hit" it, 1x script which handles OnTriggerEnter2d(bullet) ) Animated with Mecanim - simple animation to rotate the hand slightly
    ---- right hand - same as left hand
    ---- sensor = empty gameobject - with a separate collider, that handles if "fire" or another type of "danger"(but not a bullet) is near character

    The question is: do I need kinetic rigidbody2d on left hand, right hand, torso, sensor? On my test it is working without rb2ds, but I cannot tell if Unity is making a physics recalculation because of the GOs("body parts") with colliders but missing corresponding rb2d? The direct answer to that question will solve the thread, can you help @MelvMay?
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    2,049
    They don't.

    In your example, the left/right hand colliders are on children GameObjects of the 'torso' so as I've already said, they'd attach to a parent Rigidbody(2D), in this case the Dynamic one on the 'torso' GameObject. If you move the left/right hands, the colliders on them will need to be recreated and if you look at the profiler you'd see the collider recreate calls. They need to be recreated because their geometry lives in the space of the Rigidbody2D and you're modifying the Transform between the Rigidbody2D GameObject and the GameObjects the left/right hands are on.

    You should add a Rigidbody2D set to be Kinematic (always do this if you're modifying the Transform with colliders on it). The reason this helps is that the colliders live in the space of the Rigidbody. When you animate the Transform on that GameObject, it moves the Rigidbody and not the colliders so they don't need to be recreated.

    So a direct answer is yes, add Kinematic Rigidbody for performance reasons.
     
    jownas, Wampow and Tetsubo like this.
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    2,049
    BTW: You'll note that if you add a Rigidbody2D to the left/right hands and set them to be Kinematic, the Rigidbody2D will automatically move to follow the parent Rigidbody2D on the torso which is what you'd expect/want.
     
    llMarty likes this.
  8. Tetsubo

    Tetsubo

    Joined:
    Jun 26, 2014
    Posts:
    40
    Ok, sir, thank you very much for the answer!

    Btw. hands are not children of torso in my case. Torso and both hands are children of character - the parent object. Torso in my case would be chest, if that explains it a bit better. You didn't write about the "sensor", that is the empty game object. Do I need a rb2d there? As far as I understand, from what you tought me here - I don't need that, because I don't directly modify the transform of the "sensor". Is that right, @MelvMay ?
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    2,049
    Right, was confused by what you wrote up; you might want to try the bullet list when formatting in the future; helps a lot!

    If I follow the hierarchy then that collider is attached to the dynamic RB on the 'character' as are the hands. If that doesn't move independently but you want it to move with the 'character' no you don't need another Rigidbody.
     
    Tetsubo likes this.
  10. Tetsubo

    Tetsubo

    Joined:
    Jun 26, 2014
    Posts:
    40
    Perfect, that's it! Thank you for the support, patience and complete answer to all my questions!