RaZ
RaZ

Reputation: 265

Change/Flip Player Sprite direction

currently I am working on a networked 2D platformer. I am trying to make the character always face in the direction he is walking. Therefore I am using this code: (BTW I know this is not supposed to work)

if (rigidbody.velocity.y > 0) {
        transform.rotation = 0,0,0;
} else if (rigidbody.velocity.y < 0) {
        transform.rotation = 0,180,0;
}

So my question would be what code would I have to use to make the character's transform's rotation be 0,0,0 when his velocity on the y axis is over zero and 0,180,0 when its under.

Note: Yes I do know there are other ways of approaching this but I think this would be the ideal way in this instance and I am curious.

Upvotes: 1

Views: 2307

Answers (1)

Programmer
Programmer

Reputation: 125275

Ways to flip/change the direction where the character is facing:

1.You can use this variable on from the SpriteRenderer:

spriteRenderer.flipX = true;
//OR flipY for the Y-axis
spriteRenderer.flipY = true;

2.OR Multiple the axis you want to flip by -1.

Vector2 newPos = new Vector2(transform.localScale.x, transform.localScale.y);
newPos.x = newPos.x * -1; //Flip X
transform.localScale = newPos;

Upvotes: 1

Related Questions