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
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.
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!
Do you know how you do that? Because when I try to add a collider property to my animation, I can only choose between "Enabled", "IsTriggerd" and stuff like that. I don't see how I can change the collider somewehere. Would be great to get an answer, I really need it!
– KingstonHi thanks for your response, I've done exactly as you said, but when I edit the collider to match the current sprite, it changes the collider to that for all the sprites (overwrites all other collider changes). I'm not sure what I'm doing wrong, any response would be appreciated!
– SubcontraoctaveWrong answer, you can't record changes in colliders such as Polygon Collider 2D :/
– CarhartI've moved your comment from "answer" to the comment section because it actually does not answer the question. That said: I'm having the same issue and it is very annoying.
– EllanellardYou 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
.
Did you try that? You can't record Polygon2D colliders!
– Carhartit works! thank you very much mate!
– HypesthesiaI 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.
This worked very well for me! thanks for sharing the solution
– OxytocicI followed @Underestimate’s solution and it worked.
© 2022 - 2024 — McMap. All rights reserved.
This worked very well for me! thanks for sharing the solution
– Oxytocic