Search Unity

Animating Collider Properties

Discussion in 'Animation' started by templewulf, Jan 3, 2019.

  1. templewulf

    templewulf

    Joined:
    Dec 29, 2013
    Posts:
    52
    The documentation shows us that you can animate components on the object hierarchy attached to the Animator component's object. https://docs.unity3d.com/Manual/animeditor-AnimatingAGameObject.html

    Weirdly, it seems to stack all changes by type. I have multiple BoxCollider2D components to form a complex "hurtbox" shape common in fighting games. If I change all of them a little bit each frame, it just adds them together under the BoxCollider2D type, then applies them to only the first BoxCollider2D on the object.

    I thought I'd outsmart the first-component-of-a-given-type restriction by using a PolygonCollider2D, but it doesn't even capture outline changes from that type!

    • Is there a way to manipulate either multiple BoxCollider2D components?
    • Is there a way to manipulate the outline of a PolygonCollider2D component?
    • Is there a better way to animate multiple colliders through the Animation system?
     
  2. templewulf

    templewulf

    Joined:
    Dec 29, 2013
    Posts:
    52
    I decided on an approach, even though it's not architecturally ideal.

    It seems that the Animator system can't deal with collections, so that disqualifies both the list of points in a PolygonCollider2D as well as multiple BoxCollider2D components.

    The animator can adjust child objects, so I added a bunch of children to my object with the sprite animator, and each one of them has a BoxCollider2D. I made a CollisionPublisher MonoBehaviour that I attached to each of those children with a box collider. It contains a reference to an ICollisionSubscriber (implemented by my character controller) as well as a ScriptableObject that designates the type of collider it is and what color it should render in the Gizmos.

    It feels like a failure of data structure design to have a pile of objects named things like Hitbox7, but it's fairly extensible in practice.
     
  3. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Why not use a trivial component that adjusts all child components, and animate that instead?
     
  4. templewulf

    templewulf

    Joined:
    Dec 29, 2013
    Posts:
    52
    I tried that approach as well (with a constellation of ColliderAnimator, ColliderAnimation, ColliderKeyframe, and so on), but it was more to coordinate than simply packing everything into an AnimationClip.

    Over the weekend, I made a prefab of a single BoxCollider2D, a custom CollisionPublisher MonoBehaviour, and I have it send callback to the "managing" MonoBehaviour, which implements custom interface ICollisionSubscriber.

    This way just came out a little cleaner, without having to keep ColliderAnimation and regular Animation in sync.