Search Unity

Size of Object should be equal to the size of the Sprite inanimation

Discussion in '2D' started by doby420, Nov 15, 2019.

  1. doby420

    doby420

    Joined:
    Oct 28, 2018
    Posts:
    8
    Hi, Friends

    I have an animated sprite that is enemy When I apply collider of any kind it moves with the enemy but when the enemy attacks it changes its size but collider does not change its size as per sprite.
    There are 7 to 8 images in the animation. Can I change the size of the collider with each animation image of the sprite in the Animator?

    Regards,

    Untitled.png
     
  2. doby420

    doby420

    Joined:
    Oct 28, 2018
    Posts:
    8
    Any Solution Pleases?
     
  3. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    You should use primitive colliders attached to the bones and they will follow the animation.
     
    vakabaka likes this.
  4. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    I would use primitive collider too. Maybe a capsule collider. The size from the primitive collider can be controlled trough the animation. Ok, it will not exactly pass into the sprite still in most case this isn't needed.

    maybe create 7 childobjects with different colliders and enable them depended on animation ? :)
     
    Last edited: Nov 16, 2019
    MisterSkitz likes this.
  5. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    In 2D I thought the primitive colliders were box colliders or circle colliders? I did not think one could optimally compound the capsule colliders. In 3D development I believe you can use them.

    Or am I mistaking here?
     
  6. doby420

    doby420

    Joined:
    Oct 28, 2018
    Posts:
    8
    No, I checked all types of colliders even capsule collider but its not working.
     
  7. doby420

    doby420

    Joined:
    Oct 28, 2018
    Posts:
    8
    How can I do this
     
  8. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    there is capsule 2d collider, still I have looked at it and its direction can't be saved in the animation (vertical-horizontal). Also I think box collider 2d will be better.

    Take one box collider 2d. Go into the animation window, select the frame where the colliders size should be changed, enable record button and change the colliders size.
     

    Attached Files:

    doby420 and MisterSkitz like this.
  9. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Yes, there are capsule collider 2D but I thought that only the box and circle colliders were considered "primitive" colliders. In 3D I believe, if I'm not mistaking, that even a capsule collider is considered a primitive collider. I'm not so certain that the capsule colliders will adjust properly according to movement in 2D. However, I am in agreement with you about the box colliders. I think this would be the most efficient way for him to go.

    To answer his question about "How can I do this"?

    You can create an empty object and put a Physics2D box collider on it and attach it to the part you want. Then duplicate and keep putting the colliders where you want. Maybe you don't need them to be active until certain times during an animation. The code to access colliders is relatively simple so enabling/disabling them wouldn't require much coding.
     
    doby420 and vakabaka like this.
  10. doby420

    doby420

    Joined:
    Oct 28, 2018
    Posts:
    8
    awesome, Thank you you solved my problem Sir