I'm really new to using unity and C# so excuse me if this is a dumb question but I really haven't been able to figure it out..
I just want to flip my sprite in my 2D game but when I do that using localScale the location of the sprite changes too.
It's like this:
flipped right: | [sprite facing right]
flipped left: [sprite facing left] |
the "|" sign stays in one location in the game. Wow this is so hard to describe without pictures but I hope you can understand me. I can't post images because I'm new to the forum.
So the issue is: When I change the localScale the whole sprite moves and I don't understand why. Could someone please help me? Thanks in advance!
My code:
private void HandleInput()
{
if (Input.GetKey(KeyCode.D))
{
_normalizedHorizontalSpeed = 1;
if (!_isFacingRight)
Flip();
}
else if (Input.GetKey(KeyCode.A))
{
_normalizedHorizontalSpeed = -1;
if (_isFacingRight)
Flip();
}
else
{
_normalizedHorizontalSpeed = 0;
}
if(_controller.CanJump && Input.GetKeyDown(KeyCode.Space))
{
_controller.Jump();
}
}
private void Flip()
{
transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
_isFacingRight = transform.localScale.x > 0;
}
}