0
\$\begingroup\$

I have a sprite attached to the player like an accessory and it flips along with the player when moving in the opposite direction. However, due to the player being a dog, when flipped, the accessory only flips and the position doesn't change. I want to move that sprite to a bit left every time the player flips.

if (Input.GetAxis("Horizontal") < 0)
    { spriteRnderer.flipX = true; bandanaRenderer.flipX = true; }
else if (Input.GetAxis("Horizontal") > 0)
    { spriteRnderer.flipX = false; bandanaRenderer.flipX = false; }
transform.position += new Vector3(Input.GetAxis("Horizontal") * Time.deltaTime * speed, 0);
animator.SetFloat("Speed",Mathf.Abs(Input.GetAxis("Horizontal") * Time.deltaTime * speed));
jumpSpeed = 0;
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Please don't post sourcecode as images. Just copy&paste it as text from your development environment, and add 4 spaces in front of each line so it appears as a code block with syntax highlighting. That makes it possible for people to directly copy&paste from your code into their answers. \$\endgroup\$
    – Philipp
    Sep 29, 2021 at 10:40

1 Answer 1

1
\$\begingroup\$

When the horizontal axis is < 0, just rotate the player instead of flipping like this:

if (Input.GetAxis("Horizontal") < 0)
{
    transform.eulerAngles = new Vector3(0,180,0);
}
else if (Input.GetAxis("Horizontal") > 0)
{
    transform.eulerAngles = new Vector3(0,0,0);
}

The same applies to any sprite.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .