在Unity 2D中翻转2D精灵动画

本文关键字:2D 精灵 动画 翻转 Unity | 更新日期: 2023-09-27 18:27:47

我有一个关于2D精灵动画的快速问题,我在任何地方都找不到具体的答案:

我有一个精灵,右边有行走动画。然而,当他向左走时,我显然想将动画向左翻转(2D侧滚动条)。

使用transform.localscale.x,我可以很容易地翻转精灵本身,但是,它只翻转精灵。不是动画剪辑。(Unity不再发生这种情况)

因此,当精灵翻转时,动画片段开始播放的那一刻,它就会向右翻转(因为我唯一的动画片段是面向右侧的精灵)。

唯一的方法是在Photoshop中翻转精灵,还是在Unity中有方法?

谢谢!

更新:对于单位的实际版本,如果通过将变换乘以-1来缩放变换,则动画帧也会缩放。

在Unity 2D中翻转2D精灵动画

我通过这样做终于明白了:

void Flip()
{
    // Switch the way the player is labelled as facing
    facingRight = !facingRight;
    // Multiply the player's x local scale by -1
    Vector3 theScale = transform.localScale;
    theScale.x *= -1;
    transform.localScale = theScale;
}

这来自Unity的2D Platformer示例。

要使用Flip方法实现某种检查,可以执行与下面的示例类似的操作,该示例是基本的移动代码。facingRight被设置为类上的一个值,以便其他方法可以使用它,并且它默认为false

void Update() 
{
    //On X axis: -1f is left, 1f is right
    //Player Movement. Check for horizontal movement
    if (Input.GetAxisRaw ("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f) 
    {
        transform.Translate (new Vector3 (Input.GetAxisRaw ("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
        if (Input.GetAxisRaw ("Horizontal") > 0.5f && !facingRight) 
        {
            //If we're moving right but not facing right, flip the sprite and set     facingRight to true.
            Flip ();
            facingRight = true;
        } else if (Input.GetAxisRaw("Horizontal") < 0.5f && facingRight) 
        {
            //If we're moving left but not facing left, flip the sprite and set facingRight to false.
            Flip ();
            facingRight = false;
        }
    //If we're not moving horizontally, check for vertical movement. The "else if" stops diagonal movement. Change to "if" to allow diagonal movement.
    } else if (Input.GetAxisRaw ("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f) 
    {
        transform.Translate (new Vector3 (0f, Input.GetAxisRaw ("Vertical") * moveSpeed * Time.deltaTime, 0f));
    }
    //Variables for the animator to use as params
    anim.SetFloat ("MoveX", Input.GetAxisRaw ("Horizontal"));
    anim.SetFloat ("MoveY", Input.GetAxisRaw ("Vertical"));
}
void FlipHorizontal()
{
    animator.transform.Rotate(0, 180, 0);
}

您也可以在变换本身上执行此操作(无需动画师)。但在这种情况下,动画师可以覆盖旋转值

我就是这样做的——几乎与Jestus使用统一脚本的其他技术相同。

var facing : String = "right";
function updateFacing(curr : String){
    if(curr != facing){
        facing = curr;
        var theScale : Vector3 = gameObject.transform.localScale;
        theScale.x *= -1;
        gameObject.transform.localScale = theScale;
    }
}
//put to use
function controls(){
    if(Input.GetKey (KeyCode.LeftArrow)){
        updateFacing("left");
    } else if(Input.GetKey (KeyCode.RightArrow)){
        updateFacing("right");
    }      
}

如果您在Unity:中设置动画

  1. 复制要翻转的动画的所有帧(精灵)
  2. 将这些帧粘贴到新动画中,然后选择第一帧上的所有内容
  3. 将第一帧的x比例从1更改为-1
  4. 对动画的最后一帧执行同样的操作

现在它应该面向另一个方向!

这是我的C#实现。它使用一个字符串作为面向的方向,以使调试更加容易。

public string facing = "right";
public string previousFacing;
private void Awake()
{
    previousFacing = facing;
}
void Update()
{
    // store movement from horizontal axis of controller
    Vector2 move = Vector2.zero;
    move.x = Input.GetAxis("Horizontal");
    // call function
    DetermineFacing(move);
}
// determine direction of character
void DetermineFacing(Vector2 move)
{
    if (move.x < -0.01f)
    {
        facing = "left";
    }
    else if (move.x > 0.01f)
    {
        facing = "right";
    }
    // if there is a change in direction
    if (previousFacing != facing)
    {
        // update direction
        previousFacing = facing;
        // change transform
        gameObject.transform.Rotate(0, 180, 0);
    }
}