My FlipSprite Method using SpriteRenderer.flipX

[SerializeField]
float runSpeed = 5f;

SpriteRenderer _sprite;

void Start () {
rBody2D = GetComponent<Rigidbody2D>();
_sprite = GetComponent <SpriteRenderer>();
}

void MoveHorizontally(){
float controlThrow = CrossPlatformInputManager.GetAxisRaw(“Horizontal”);
Vector2 playerVelocity = new Vector2(controlThrow * runSpeed, rBody2D.velocity.y);
rBody2D.velocity = playerVelocity;

	flipSprite(controlThrow);
}

void flipSprite(float controlThrow)
{
if (controlThrow == -1) {
_sprite.flipX = true;
}
else
if (controlThrow == 1) {
_sprite.flipX = false;
}
}

Thanks for posting this. I really like how understandable this is set up.

1 Like

Thanks for sharing :grinning:, works great on keyboard. Doesn’t work with Game Pad as the horizontal move is made with analog stick (controlThrow will vary between -1 and 1, like for example 0.256f).

Privacy & Terms