unity-game-enginegame-developmentflip

How to flip a composite character in unity 2d?


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?


Solution

  • 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.