Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Setting up animated physical object (using mesh colliders)

Discussion in 'Editor & General Support' started by Phenomonaut, Sep 29, 2014.

  1. Phenomonaut

    Phenomonaut

    Joined:
    Sep 30, 2012
    Posts:
    61
    I'm having trouble figuring out how to set up a simple animated object that is also physical. I've built an animated object for testing purposes that does this:



    It has two bones, two collision volumes, and one mesh. I want it to collide with a ground plane so that it kinda pushes itself up using real-time physics. I know I can use box colliders in Unity for the meshes, but eventually this will be a mesh that requires custom collider shapes, so I've added boxes into the mesh to be used as mesh colliders. So my hierarchy looks like this:



    I need someone to help with two things:
    1. Have I created this hierarchy correctly?
    2. How do I set this up in Unity so that it is physical and moves around by itself?
    I know I need rigidbodies (probably on the collision volumes?), and I know I need mesh colliders. But my attempts to set this up haven't worked and nothing is colliding correctly. If it helps, here is a text version of my hierarchy:

    • Mesh
    • Root_Bone
      • Arm_Bone
        • Arm_Collision
      • Root_Collision
     
    Last edited: Sep 29, 2014
  2. Razorwings18

    Razorwings18

    Joined:
    May 8, 2014
    Posts:
    69
    How your object behaves in an animation is independent of how it behaves when it comes to physics. Your object can play the animation, but once your Animator/Animation component is disabled, the physics engine will take over.

    To achieve the movement you show in your example by physics, what you would do is:
    - Attach a Rigidbody to your Root_Bone
    - Freeze Root_Bone's Rigidbody position (in x, y and z) so it stays put. May want to freeze all Rotation as well, so it won't wobble.
    - Attach a collider (box in this case, could be Mesh in your final product) and Rigidbody to your Arm_Bone
    - Freeze your Arm_Bone's Rigidbody rotation for X and Y (this may be different in for you depending on how you positioned the object). This is so it only rotates on one axis.
    - Add a Hinge Joint to Arm_Bone conected to Root_Bone's Rigidbody.
    - Configure the Hinge Joint. Some ideal values would be Anchor at (0,0,0), Axis Z, Connected anchor (0,0,0.5) and no limits.
     
    Phenomonaut likes this.
  3. Phenomonaut

    Phenomonaut

    Joined:
    Sep 30, 2012
    Posts:
    61
    Thanks for taking the time to go through this. To make things simpler, I removed the mesh collision parts and just created box colliders. Using your instructions, I'm getting close to a successful test. I had to do two additional things:
    • Add a box collider to the Root_Bone.
    • Lower the mass of the Arm_Bone rigidbody to keep it from flying off into space.
    It seems like the Arm_Bone collision gets immediately sunk into the plane and causes the physics to freak out. Changing the collision detection on its Rigidbody component doesn't help:



    Hopefully more tweaking will get it to stay still and just roll around.
     
  4. Razorwings18

    Razorwings18

    Joined:
    May 8, 2014
    Posts:
    69
    I can't quite tell by the gif, but it does seem like you got the joint right. You are definitely missing the freezes in position an rotation though.
     
  5. Phenomonaut

    Phenomonaut

    Joined:
    Sep 30, 2012
    Posts:
    61
    Oh, I don't think I was clear... I want the whole object to be physical so it moves around on its own. Something like this (or whatever the specific behavior would be in Unity):



    I think I'm close if I can figure out why the arm is interpenetrating with the ground plane in my previous gif.
     
  6. Razorwings18

    Razorwings18

    Joined:
    May 8, 2014
    Posts:
    69
    Well, here are a couple of ideas:
    - Your object may be turning too fast, so when physics are calculated the first time after the collision takes place, it has already passed the ground a good deal, which prompts it to be rejected with a high force.
    Play with EDIT > PROJECT SETTINGS > TIME, "Fixed Timestep". Modifying > PHYSICS, "Min Penetration For Penalty" and "Solver Iteration Count" as well may have an effect.

    - Change the Collision Detection of your object colliders to Continuous

    Be aware that these changes will impact performance.
     
    Phenomonaut likes this.
  7. Phenomonaut

    Phenomonaut

    Joined:
    Sep 30, 2012
    Posts:
    61
    Success! An autonomous, animated, physical shape!



    Thanks again, Razorwings! The one additional thing I did that may (or may not) have helped is that I moved the Hinge Joint component from the Arm_Bone to the Root_Bone.
     
    Razorwings18 likes this.