• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Captain_Dando · Nov 14, 2013 at 12:03 PM · 2dcollider

How can I change my polygon collider to correctly represent each frame in a sprite?

Hi everyone, I've begun using the 2d tools in unity 4.3 and I've run into a bit of a snag. When I attach a polygon collider to my sprite, sure enough, the polygonal shape represents the sprite, but it doesn't change as the sprite animates. Is this achievable or am I using the component for something it's not intended for?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Guidez · Dec 07, 2013 at 02:43 AM

As of Unity 4.3, I believe the only way to do this at this time is to manually create a new Polygon collider for each frame in the animation. Attach all the polygon colliders to your sprite, and enable/disable them as needed throughout the animation with the animation controls.

This obviously won't work well for "smooth" animations, but animations based on sprite sheets it should do just fine. If this is needed for, say, a swinging sword, typically attaching a BoxCollider to an empty GameObject and animating the transform of the empty is the way to go.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
3

Answer by basavaraj_guled · Jan 30, 2016 at 10:25 AM

just attach the below code to ur game object which have the animation

 public bool iStrigger ;
         //public PhysicsMaterial2D _material ;
     
         private SpriteRenderer spriteRenderer;
         private List<Sprite> spritesList;
         private Dictionary<int, PolygonCollider2D> spriteColliders;
         private bool _processing ;
     
         private int _frame ;
         public int Frame {
                 get { return _frame; }
                 set {
                         if (value != _frame) {
                                 if (value > -1) {
                                         spriteColliders [_frame].enabled = false;
                                         _frame = value;
                                         spriteColliders [_frame].enabled = true;
                                 } else {
                                         _processing = true;
                                         StartCoroutine (AddSpriteCollider (spriteRenderer.sprite));
                                 }
                         }
                 }
         }
 
         private IEnumerator AddSpriteCollider (Sprite sprite)
         {
                 spritesList.Add (sprite);
                 int index = spritesList.IndexOf (sprite);
                 PolygonCollider2D spriteCollider = gameObject.AddComponent<PolygonCollider2D> ();
                 spriteCollider.isTrigger = iStrigger;
                 //    spriteCollider.sharedMaterial = _material;
                 spriteColliders.Add (index, spriteCollider);
                 yield return new WaitForEndOfFrame ();
                 Frame = index;
                 _processing = false;
         }
     
         private void OnEnable ()
         {
                 spriteColliders [Frame].enabled = true;
         }
     
         private void OnDisable ()
         {
                 spriteColliders [Frame].enabled = false;
         }
 
         private void Awake ()
         {
                 spriteRenderer = this.GetComponent<SpriteRenderer> ();
         
                 spritesList = new List<Sprite> ();
         
                 spriteColliders = new Dictionary<int, PolygonCollider2D> ();
         
                 Frame = spritesList.IndexOf (spriteRenderer.sprite);
         }
     
         private void LateUpdate ()
         {
                 if (!_processing)
                         Frame = spritesList.IndexOf (spriteRenderer.sprite);
         }

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image darthdeus · Jul 15, 2016 at 09:59 PM 0
Share

I'm confused, where does this actually load the polygon colider from? Don't you have to create them by hand?

avatar image $$anonymous$$ · Feb 11, 2017 at 12:54 AM 0
Share

actually I got this to work, just add it to your ObjectController.cs file inside
public class myOjectController : $$anonymous$$onoBehavior { after your own code, you can see inside private IEnumerator AddSpriteCollider where the pollygon collider is made for each sprite

avatar image jdean300 $$anonymous$$ · Feb 11, 2017 at 01:00 AM 0
Share

It's incomplete. The posted code needs to be included in a class that you define:

 public class $$anonymous$$yClass : $$anonymous$$onoBehaviour{
   // posted code here
 }
avatar image Essexrookie · Feb 15, 2019 at 12:55 AM 0
Share

This works perfectly!

avatar image
1

Answer by GarBenjamin · Feb 16, 2014 at 04:00 AM

I know this is delayed but it might be helpful... I just ran into this same issue with my first Unity3D game project which is actually in 2D using the new 2D support offered by Unity 4.3.

Anyway, what I came up with is to dynamically generate the Polygon2D colliders and attach them to my objects during run-time.

I just didn't want to spend the time trying to manually create the colliders for each sprite. That seems like a lot of effort & time to me. In reality, I think the Unity2D system should be creating the colliders and attaching them to the sprite images themselves not the game object. That way when we animate the game objects the colliders will have been prepared beforehand and automatically use the appropriate collider for the current sprite in the animation sequence. Since Unity3D does not actually work that way I decided to write the code to do it for my project.

Basically, all I do is track the list of Polygon2D colliders attached to my game objects. Whenever my game objects sprite (strange terminology... I am "old school" and sprites to me are the game objects... what Unity3D calls sprites I simply call images or animation frames) anyway, when the image changes I...

  • Disable the current active collider.

  • Check my list for an existing collider already attached to handle this image.

  • If a collider already exists for the image then I simply activate this existing collider.

  • If no collider is found for the image then I create a new collider, activate it and add it to my tracking list.

This works fine for my needs. Some of my enemy sprites are quite large (1/4) of the screen and I don't need many onscreen at one time. Prefer to have less quantity of enemies but make them more intelligent / interesting.

My game won't ever have more than 16 enemies onscreen at any one time. However, I did test it with 200 enemies and the fps never dropped. This is on my laptop I bought back in 2008 so I think handling the colliders this way is reasonably efficient.

Also, Guidez said "Attach all the polygon colliders to your sprite, and enable/disable them as needed throughout the animation with the animation controls." I am assuming they mean by setting flags or triggers. As far as I know there is now way to directly map colliders to the states and automatically enable / disable the colliders as part of the animation or states. But maybe I missed that part.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image adrianrodriguez · Sep 25, 2014 at 12:43 PM 0
Share

I'm super late to this, but I was thinking about the same question as the OP, and wanted to know if you had an example of this somewhere?

avatar image GarBenjamin · Oct 01, 2014 at 12:11 PM 0
Share

Hey adrianrodriguez. Just noticed your comment. It just so happens that last night I posted the code for how I implement this on the Unity forum. I actually strramlined it since my answer here. Now the colliders are generated all at one time during the initialization phase of the game ins$$anonymous$$d of creating a collider each time a new frame is displayed for the first time in game. You can check it out here: http://forum.unity3d.com/threads/is-it-better-to-use-sprite-swapping-or-animations.271278/#post-1793895

avatar image PabloUnityArgentina GarBenjamin · Oct 05, 2015 at 07:46 PM 0
Share

Thank you very much GarBenja$$anonymous$$ i will try your code.

avatar image
0

Answer by CarterG81 · Feb 11, 2017 at 05:45 AM

This is definitely achievable.

Although this is not a real answer, but it should be documented; 2D ColliderGen has this as a feature.

https://www.assetstore.unity3d.com/en/#!/content/7540

alt text


9148bf8e-2958-42c1-a769-2e8ffc3c8bb4-scaled.jpg (105.0 kB)
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image $$anonymous$$ · Feb 11, 2017 at 11:17 PM 0
Share

What advantages does this 25$ assets have over the code in comment on the answer above in regards to just auto generating a new poly collider?

avatar image CarterG81 · Feb 12, 2017 at 03:36 AM 0
Share

Idk. that's best a question for the developer of the asset. I just wanted this to be known, since I think the asset store is so helpful.

avatar image $$anonymous$$ CarterG81 · Feb 13, 2017 at 05:17 PM 0
Share

ok, understandable, but have you used this asset? i wouldn't $$anonymous$$d supporting the creator if there is something to gain vs free code

avatar image CarterG81 $$anonymous$$ · Feb 13, 2017 at 07:37 PM 0
Share

I do use this asset, which is why I felt comfortable referring others to it. However as always with highly rated assets, it is always a matter of time vs money. "Free code" means doing it yourself, which takes time. Buying an asset, if a simple feature or well documented, saves time. Just ask yourself, how much do you value your time and effort? And can you benefit from its other features too?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can a character not move out of box collider's bounds? 1 Answer

Top Down 2d (PC): Gameobjects' Colliders intermeshing 1 Answer

How do I prevent characters from being able to land on each others heads? 1 Answer

[Tilemap] Can you spawn a Prefab (with colliter) together with a tile when placing it? 0 Answers

collider vs collider2d 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges