Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Would it be possible to automaticly update the 2Dphysic based on the current sprite animated ?

Discussion in 'Physics' started by Kaiymu_, Nov 28, 2019.

  1. Kaiymu_

    Kaiymu_

    Joined:
    Jun 1, 2016
    Posts:
    47
    Hello everyone,

    Currently I'm working on a 2D game with some small player animation (Attack, idle ... etc).
    On the SpriteEditor, I set a specific shape for each of theses animations.
    When I play my animation, I want my 2DCollider to update automaticly when a sprite is being set.

    Is their an automatic way to do that ? Otherwise each frame I'm ressetting the collider and it's working, but I feel like it's ugly and consomes a lot for "nothing".

    A workaround would be to have an event being triggered when a new sprite / animation would be played.

    Thank you.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,316
    Physics just isn't designed to work with colliders being "animated" or in more accurate terms, constantly changed. It's not so bad when being used for triggers but for other types of collisions, particularly if they're being used to support a character on a platform, it's a terrible idea. Know that as soon as you disable/delete/change a collider its existing contacts are immediatley destroyed and will have to be calculated again during the next step.

    So like everything in Unity, we allow you to do it but it isn't something that scales well.
     
  3. Kaiymu_

    Kaiymu_

    Joined:
    Jun 1, 2016
    Posts:
    47
    So, would you recommend to just have "basic" shape surrounding the character and only update when an animation is done ?
    Let's say a "BoxCollider" that becomes more rectangle when you crouch, for exemple ?

    Thank you for the input.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,316
    I don't recommend modifying them at all but of course it's required in some cases. Just keep an eye on performance if you expect to do this at scale. Again, take note of what I said if these colliders are being used to support the character as you're effectively changing the conditions the solver is finding each time and causes overlaps that it then has to solve. Easy to cause jittery motion like this.
     
    Kaiymu_ likes this.
  5. Kaiymu_

    Kaiymu_

    Joined:
    Jun 1, 2016
    Posts:
    47
    Okay !
    Thank you for the feedback, means a lot :)
    I'll be careful with Collision ;)
     
  6. URocks

    URocks

    Joined:
    May 1, 2014
    Posts:
    159
    But why is deformation coliders not implemented. Unity acquired Uni2d whis suppirted it. I also want to use deformed coliders from rigging and i cant. Here you can see how it was working in Uni2d
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,316
    No matter what it is called, it's not "deforming" the geometry, it's recreating those colliders and it isn't good for performance at scale. However you are free to use the SpriteShape which came out in 2018 which can recreate colldiers to match its shape too. Note that this isn't physics, it's a 2D feature.

    Anything, including yourself can "deform" (read that set an EdgeCollider2D or PolygonCollider2D) to whatever you like. The SpriteShape does this to as just another user of physics. What I'm trying to get to is that there isn't a "deformable" collider because that's just another way of saying changing the geometry.
     
  8. URocks

    URocks

    Joined:
    May 1, 2014
    Posts:
    159
    Thanks Melv I understand what do you mean. But even to have slower performance option is better than no option. What I am missing is to somehow add 2d edge or poly collider on rigged and deformed sprites. I didnot find any option to get deformed verticles so I could not hack it to the colliders. Do you have any idea how I can do it? I am not sure if spriteshape will help me