1
\$\begingroup\$

I've created an animation by using different frames. Then applied polygon collider on that object. Problem is that when frame changes in animation, it does not reset collider. I've searched for resetting polygon collider as sprite gets change. One solution I've found is like following : Destroying existing collider & Adding new collider to the object. http://answers.unity3d.com/answers/723811/view.html I've applied this on the object. But it hangs the game. Is there any another way to reset polygon collider from c#.

\$\endgroup\$
2
  • \$\begingroup\$ You could create PolygonColliders for each frame and store them in some kind of data structure (frame + collider). Then when you animate just set the new frame and replace the collider \$\endgroup\$
    – floAr
    Jul 1 '16 at 7:53
  • \$\begingroup\$ I don't thing there is such thing as "reset". When you modify a collider that's it. You can do as floAr said, or you can store point coordinates of your polygon collider for each frame(say a list of points for each frame), or you can try to find a way to animate some kind of multi-collider shape (of course all colliders under one rigidbody(2d) so they are treated as the same physical object) BTW you can check if collider shape can't be animated too. \$\endgroup\$
    – Nikaas
    Jul 1 '16 at 8:13
1
\$\begingroup\$

Use combination on colliders in a child object. Animate enable-disable which collider you need or not in which frame with the main animation.

enter image description here

You can't animate similar collider used over once. But you can use multiple child objects containing similar collider.

\$\endgroup\$
1
\$\begingroup\$

I've got an solution for this problem from below link : http://answers.unity3d.com/answers/1135594/view.html

\$\endgroup\$
1
  • \$\begingroup\$ Link-only answers are discouraged on StackExchange. Because we can't control what happens on an external site, this linked resource could change address or be deleted, leaving this answer useless to future visitors. The recommended practice is to at least summarise the main points/steps described in the link, providing the original source for credit/further information. Of course, the more information you can provide in the body of the answer, the better — then you can save future users a click. :) \$\endgroup\$
    – DMGregory
    Sep 5 '16 at 12:34
1
\$\begingroup\$

I know this is old but I was recently looking for something like this. Anyway, I figured it out: you could pull the physics shape directly from the sprite, then shove it straight into the polygoncollider, so the code below is all you need to do that. This gives you the benefit of being able to directly customise the colliders directly in the sprite editor custom physics collider tab, and late update means it's always frame perfect.

PolygonCollider2D col;
SpriteRenderer spriteRenderer;
List<Vector2> physicsShape = new List<Vector2>();
// Start is called before the first frame update
void Start()
{
    col = GetComponent<PolygonCollider2D>();
    spriteRenderer = GetComponent<SpriteRenderer>();
}
void LateUpdate()
{
    spriteRenderer.sprite.GetPhysicsShape(0, physicsShape);
    col.SetPath(0, physicsShape);
}
\$\endgroup\$
1
  • \$\begingroup\$ Just keep in mind there's no physics step between late update and rendering, so if you swap to a shape that collides with something nearby, the physics engine won't intervene to move then apart until next frame, and you'll see penetration for a frame unless your script handles resolving these overlaps. \$\endgroup\$
    – DMGregory
    Apr 26 '20 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.