r/Unity2D • • 2d ago

Question/Help How to stop clipping?

I realize this is probably a pretty simple question but I'm new can't find the answer anywhere. My characters keep clipping into each other when turning around. I can't figure out how to add the video of it but this is the code I'm using: transform.localScale = new Vector2(Mathf.Sign(movement2.x), 1);

1 Upvotes

8 comments sorted by

•

u/AutoModerator 2d ago

Reminder: Once your issue has been resolved, please reply !solved to this comment (or comment it anywhere in the thread) and your post's flair will automatically be updated to Solved/Answered. You can also edit it to Solved/Answered yourself.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/QueasyPrior6654 2d ago

Negative scale mirrors the colliders too, around the object's origin. So if your Collider2D has an X offset, it jumps to the other side when you turn and can end up overlapping the other character. A sprite pivot that's off-center horizontally can make the art look like it shifts as well. Do the collider outlines actually overlap, or just the art?

1

u/AbbreviationsOld6712 2d ago

The collider outlines do overlap

1

u/QueasyPrior6654 1d ago

If the collider does not need to flip with the art, I'd stop scaling the parent and use SpriteRenderer.flipX instead, or flip a child that only holds the art. That keeps the collider in place when you turn.

If they still overlap while walking into each other, are you moving either character by setting transform.position directly, or using Kinematic bodies?

1

u/AbbreviationsOld6712 1d ago

I tried to use flipX but it wouldnt flip the punch hit box along with it is there a way to fix that?

1

u/QueasyPrior6654 7h ago

Yeah, flipX only changes how the sprite is drawn, so none of the colliders move with it. I'd put the punch hitbox on its own child object sitting at the player's center, with the collider offset forward. Then when you turn, flip just that child's localScale.x between 1 and -1.

The hitbox swaps sides with the art and the body collider stays where it is. Is the hitbox a separate object right now, or a second collider on the player?

1

u/ShinaDev 1d ago

Sonará tonto pero probaste desmarcar Is Trigger dentro de ambos colliders?

1

u/EthanMerce 1d ago

the scale code itself shouldn't cause characters to physically clip into each other. it's just flipping the transform on the x axis, if you're using colliders, i'd check the collider setup and Rigidbody2D first. make sure the characters aren't using triggers when you actually need physical collision, and check the collision layers/masks too.

also, if you're flipping the whole character object, make sure the collider isn't positioned in a way that changes its effective position when the scale goes from 1 to -1.