简体   繁体   English

在Unity 2D中翻转2D Sprite动画

[英]Flipping a 2D Sprite Animation in Unity 2D

I've got a quick question regarding 2D Sprite animations that I haven't been able to find specifically answered anywhere: 我有一个关于2D Sprite动画的快速问题,我无法在任何地方找到具体答案:

I have a sprite with walk animations to the right. 我有一个带有步行动画的精灵到右边。 However, I obviously want to flip the animation to the left when he walks left (2D side-scroller). 但是,当他向左走(2D侧卷轴)时,我显然想将动画向左翻转。

I can easily flip the sprite itself, using transform.localscale.x , however, that only flips the sprite. 我可以使用transform.localscale.x轻松地翻转精灵本身,但是,只翻转精灵。 Not the animation clip. 不是动画片段。 (This no longer happens in Unity) (这不再发生在Unity中)

So, while the sprite flips, the minute the animation clip begins playing, it flips back right (as the only animation clip I have is for the right facing sprite). 因此,当精灵翻转时,动画片段开始播放的那一刻,它会向右翻转(因为我拥有的唯一动画片段是面向右侧的精灵)。

Is the only way to do this to flip the sprites in Photoshop, or is there a way to do it in Unity? 这是在Photoshop中翻转精灵的唯一方法,还是有办法在Unity中执行此操作?

Thanks! 谢谢!

UPDATE: With the actual versions of unity if you scale the transform by multiplying it by -1 , the animation frames are also scaled. 更新:对于实际版本的单位,如果通过将变换乘以-1来缩放变换,则动画帧也会缩放。

I finally figured it out by doing this: 我终于通过这样做弄明白了:

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;
}

This is from Unity's 2D Platformer example. 这是来自Unity的2D平台示例。

To implement some sort of checking which makes use of the Flip method, you can do something similar to the below example which is basic movement code. 要实现某种使用Flip方法的检查,您可以执行类似于以下示例的操作,这是基本的移动代码。 facingRight is set as a value on the class so that the other methods can use it, and it is defaulted to false . 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);
}

You could also do that on transform itself (without animator). 你也可以在变形时做到这一点(没有动画师)。 But in that case rotation value can be overriden by animator 但在这种情况下,旋转值可以被动画师覆盖

This is how I did it - almost the same as the other technique by Jestus with unity script. 这就是我这样做的方式 - 几乎与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");
    }      
}

If you're animating in Unity: 如果你在Unity中制作动画:

  1. Copy all frames (sprites) of the animation that you want to flip over. 复制要翻转的动画的所有帧(精灵)。
  2. Paste those frames into your new animation and select everything on the first frame. 将这些帧粘贴到新动画中,然后选择第一帧上的所有内容。
  3. Change the x scale of the first frame from 1 to -1. 将第一帧的x比例从1更改为-1。
  4. Do the same thing with the very last frame of your animation. 对动画的最后一帧做同样的事情。

Now it should play facing the other direction! 现在它应该朝着另一个方向发挥!

This is my C# implementation. 这是我的C#实现。 It uses a string as the direction facing to make it a little easier to debug. 它使用字符串作为方向,使其更容易调试。

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM