Search Unity

Question Box Collider2D does not rotate when flipping the sprite

Discussion in '2D' started by Vandereck, Apr 29, 2023.

  1. Vandereck

    Vandereck

    Joined:
    Oct 16, 2022
    Posts:
    12
    Hi Everyone,

    I have a player empty game object with a sprite as child object, this sprite is basically a character with a sword and it has an animator component and an empty child game object with a box collider 2d.

    During the attack animation I have animated the box collider 2d to follow the sword movement which works very well, however when I use spriteRenderer.FlipX to face the direction of movement the box collider 2d stays in the same position.

    What I'm trying to achieve is having the box collider 2d following the sword either if I'm facing left or right.

    Any advice?

    Many thanks.
     
  2. Vandereck

    Vandereck

    Joined:
    Oct 16, 2022
    Posts:
    12
    For who has a similar issue. I fixed the problem by using spriteRenderer.transform.localScale = new Vector3(-1, 1, 1) rather than spriteRenderer.FlipX = true.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,456
    Yes, flipping the local scale applies to all components on that GameObject and its children that care about such stuff. Flipping the rendering on the SpriteRenderer only does that; it doesn't "talk" to other components because they're not related to it at all.
     
    Vandereck likes this.
  4. Vandereck

    Vandereck

    Joined:
    Oct 16, 2022
    Posts:
    12
    Thank you for the explanation, I still was not sure of the reasons behind it.