Hi, I have attached a 2d collider to my game object, but when animation is playing the collider is not updating, can anyone tell me how to update collider with the animation?
Thanks
By your question, I assume you already know how to make an animation, and add it to a controller.
When editing the animation, or when you’re adding frames, you should notice the record button is selected. While this is set, you can change other properties for the controller’s object. Which means, you can adjust the collider size to fit, at each frame.
Colliders don’t automatically adjust to fit the individual sprite frames.
Hope this helps!
You have to add a Collider2D
to the GameObject
(such as BoxCollider2D
), and then on the animation pane, click the record button.
Then for each frame you want to change the collider, click the "Edit Collider"
button on the collider’s component in the Inspector pane, and adjust it to what you need.
The animation has no "collider"
property. The Collider
is a Component
on the GameObject
that has the Animator
.
I didn’t imagine that unity had this problem, it’s not possible to make fluid animation, because the box collider, a capsule collider or other doesn’t follow the animation, and if you do step by step, you never record properly, and when you record, you record for others animations
Set up as many child objects as different colliders you have, for each one, place the collider to fit the animation. While animating, active the corresponding child object…
Note, this does not work with Polygon Colliders 2D.
You should see the properties show up under the animation and have their own keyframe icons if that property is in fact editable.
I tried capsule and box collider and those show up properly on the animation. The polygon collider only supports altering the offset, I suspect its because points are an inner item.
EDIT
For anyone finding this in the future, here’s a solution I went with.
Using animation events, I created a function that swaps the points of a collider based on other template colliders:
public class HeadColliderManager : MonoBehaviour
{
[SerializeField] private List<PolygonCollider2D> colliderSteps;
[SerializeField] private PolygonCollider2D rootCollider;
[SerializeField] private int colliderStep = 0;
public void Advance()
{
PolygonCollider2D next = colliderSteps[colliderStep];
Vector2[] tempArray = (Vector2[])next.points.Clone();
rootCollider.SetPath(0, tempArray);
}
}
Each frame also sets a property for which collider index should be referenced, then my object has children objects which are a disabled sprite and a polygon collider with the correct shape.