- Home /
Common 2D Collider flip problem
Hello,
I have a 2Dsprite character with an attack animation,and the colliders which are attached to it move with the sprites, that is till I flip my player in -x axis then suddenly during animation my colliders move strangely,they shrink/grow and go in the wrong direction .
dir1 and dir2 file will describe better my situation .
What solution do I have ? More people had same issue but no answer.
Thank You !
void flipScene()
{
facingRightDirection = !facingRightDirection;
Vector3 scale = transform.localScale;
scale.x*=-1;
transform.localScale= scale;
}
If you flipped your sprite by rotating it 180 degrees on the 'y' rather than change the local scale, do you have the same issues?
Despite the amount of topic on this issue, there's been no answer.. I have the same problem, and it happens whenever you try to flip (negative scale on the X axis)/rotate a gameobject having child with 2D colliders... Nothing seems to work, can somebody help ?
@$$anonymous$$anapesh - I cannot reproduce the problem. I would not be surprised if there are problems using negative x scale. But I wrote a quick test using two objects with polygon colliders and flipped one 180 on the 'Y' (tried flipping both empty game object parent and flipping child with a local offset from parent). Collisions worked fine and I saw no shrinking or growing of the collider. $$anonymous$$aybe you need to open a new question with a simple, reproducible scenario using a 180 rotation on the 'y'.
Answer by Janibasha · Jun 19, 2017 at 01:31 PM
Just change the game object localscale to -1 at a specific condition
transform.localScale = new Vector3(-1, 1, 1);
Answer by DricoJD · Mar 02, 2014 at 02:17 PM
I can't seem to re-create this in unity. Colliders should just flip with the sprite aswell, but movement is hard to make colliders move, can you post me your code
Try using a collider that covers the whole body ins$$anonymous$$d? Why do you need seperate ones, it wont make the game more appropriete?
the problem is why are they behaving abnormally when I flip my sprite . They work as expecting during the animation , but when I flip my sprite they just behave strangely.
Are these collider all attached to your sprite or are they children? Is the sprite a child of another gameobject? If you manually rotate it in the scene view, do the colliders rotate as they should?
Also, have you looked at
child.localScale = parent.transform.localScale;
foreach (Transform child in transform) {
child.localScale = parent.transform.localScale;
}
Answer by Psynbiotik · Jun 22, 2014 at 12:04 AM
This is the closest I've found to an answer:
http://answers.unity3d.com/questions/581430/how-to-properly-flip-2d-character-with-new-unity43.html
Other possible solutions:
Remove rigid body on child object (this worked for me), rigid body is causing this issue.
Disable collider before changing parent, then re-enable when done.
Answer by ClearRoseOfWar · Sep 18, 2016 at 01:37 PM
I know some people don't like old threads to be opened.. but this was the top link on google, and also the first one I checked for an answer and couldn't find one here.
Here's what worked for me: Change the sprites scale to -scale. Or even the parent Gameobjects scale
Source: https://feedback.unity3d.com/suggestions/flip-x-and-y-to-polygon-collider-2d
Your answer
Follow this Question
Related Questions
negative scale+rotation+collider 1 Answer
How to change child's rotation whithout affecting it's scale? 1 Answer
how to scale object to fit inside a room 1 Answer
How to rotate a mesh collider? 0 Answers
Animation affects the rotation.. 1 Answer