fbpx

 

Fixing Sprite posit...
 

Fixing Sprite position when Flipping 2DCollider  

   RSS

0

Hi, I'm doing the Dungeon Escape course and I want my character's hitbox to spawn only in front of him, so it won't hit an enemy behind the Player.

The problem is that the 2DCollider stay in the same position if I move Left.

After some digging I found out that the transform.localScale flips the gameObject and every children object, but when my Player moves Left, the localScale.x keeps alternating between 1 and -1. It works fine when moving Right.

 

Here's my code:

Vector3 newScale = transform.localScale;

transform.localScale = newScale;
if (move > 0)
{
newScale.x *= 1;


} else if (move < 0)
{
newScale.x *= -1;
}
transform.localScale = newScale;
Debug.Log("LocalScale: " + transform.localScale);
This topic was modified 1 month ago by chocobo

“It’s the questions we can’t answer that teach us the most. They teach us how to think. If you give a man an answer, all he gains is a little fact. But give him a question and he’ll look for his own answers.” - Kingkiller Chronicle

Topic Tags
1 Answer
0

I've fixed it. The problem was that every time the Player moves Left, it will multiply localScale.x by -1, so obviously it will alternate from -1 to 1.

So I reduced the code to:

 

Vector3 faceLeftScale = new Vector3(-1, 1, 0);
Vector3 faceRightScale = new Vector3(1, 1, 0);

if (move > 0)
{
transform.localScale = faceRightScale;
} else if (move < 0 && !_facingLeft)
{
transform.localScale = faceLeftScale;
}

“It’s the questions we can’t answer that teach us the most. They teach us how to think. If you give a man an answer, all he gains is a little fact. But give him a question and he’ll look for his own answers.” - Kingkiller Chronicle

Share:

 

 

Scroll to Top

Please Login or Register