How to flip a composite character in unity 2d?

Viewed 129

I really need help with Unity 2D. The goal is for the player to flip when going left and back when going right. There are many options on the Internet how to do this through sprite rendering. But the problem is that my character consists of parts, since the animation is bone. Each part can be flipped. And when you turn on flip in the sprite rendering of the object itself, nothing works. The question is how to make a coup at once the entire character? And if not, how to do it in parts?

3 Answers

You should flip via scale, it will effect the all components.It's also metioned in the docs.

As oistikbal said, use scaling like this:

void Update()
{
    if (Input.GetKeyDown(KeyCode.A))
    {
        transform.localScale = new Vector3(-1, 1, 1); ;

    }

    if (Input.GetKeyDown(KeyCode.D))
    {
        transform.localScale = new Vector3(1, 1, 1); ;

    }
}

When A/D is pressed the scale is changed so the character is flipped.

I'm sure you are familiar with Input.GetAxisRaw ("Horizontal") in the tutorial. Return the character in the simplest way:

var xAxis = Input.GetAxisRaw("Horizontal");

if (xAxis != 0) transform.localScale = new Vector3(xAxis, 1, 1);

In This script, if Axis is not 0 (no pressed key or stop pressing), sets the ScaleX of the sprite equal to the xAxis direction.

Related