Search Unity

Making ants crawl up/down hills smoothly

Discussion in '2D' started by shopguy, Dec 13, 2014.

  1. shopguy

    shopguy

    Joined:
    Jan 12, 2013
    Posts:
    282
    I'm wondering if Unity, physics or mecanim has much to help me with the task of making characters smoothly walk along uneven surfaces. I'd like to use bone based (mecanim) motion for several ants (and other insects/etc) traveling along uneven terrain. I guess the legacy animation system would work fine as well, but I want them to be very smooth so not likely going to use frame based animation (sprite swapping).

    Untitled.png

    With the physics system I could rig the characters with hinge joints for legs, and polygon colliders and rigidbodies, and I guess try to simulate actual walking by applying force to various parts... but um, that feels very complex and likely to kill performance (I'd like to have 10 to 30 insects in view).

    I've only played around with mecanim and followed the tutorials, but that was maybe 2 years ago. I think it is based on character controllers and not rigidbodies... and might also have performance issues, if even ideal for this usage.

    I'm guessing I'll need to come up with some custom controllers that rotate my characters, probably some raycasting towards the ground to decide what slope of terrain is under them... just wondering if there is anything easier that I'm missing, before I spend (possibly) many days on this one part of my game.
     
  2. AdamBradders

    AdamBradders

    Joined:
    Mar 26, 2014
    Posts:
    8
    That's the most common approach I think - you'll need to get the normal of the surface they're walking on. Do a series of raycasts along the base of the character to act as a set of ground sensors. The collision between the raycast and the ground should give you a normal vector for that collision. If you add up the normals from each raycast and average them, you'll get an up vector for your characters transform that represents the average slope of the ground it's walking on. It's not as complex as you'd think, it's pretty straightforward. I think it's not that expensive either, you can probably distribute the workload over several frames.

    this article came to mind when thinking of this; http://www.asteroidbase.com/devlog/7-learning-how-to-walk/ so check that out.

    As for the animation for your little ant dudes, I don't know a huge amount about that, perhaps someone else can chime in. The only term that comes to mind is Inverse Kinematics, which should allow you to modify the pose of your animation such that you can have a canned walk cycle for the ants, and then modify the animation at runtime such that the legs meet the floor in the correct places. But as I say, it's not something I know much about, unfortunately.
     
  3. brendan-vance

    brendan-vance

    Joined:
    Jan 16, 2014
    Posts:
    36
    A circle collider is generally the best way to get smooth ground collisions out of Box2D. You might try using just one circle collider per ant and using the contact normal contained in its OnCollisonStay callbacks to orient your graphic properly.
     
  4. shopguy

    shopguy

    Joined:
    Jan 12, 2013
    Posts:
    282
    Thank you both, some good info here. I had just started working on the ray method, but I didn't know the circle collider had the "contact normal" info, I'll check that out as well. I ended up purchasing "Puppet2D" from the asset store, thinking it might help with both issues. Looks like it will help with the animations, and probably the IK, but nothing really with the collision (don't fall through ground) or rotating body to match ground.