Flipping a 2D Sprite Animation in Unity 2D

后端 未结 5 1103
失恋的感觉
失恋的感觉 2020-12-30 07:04

I\'ve got a quick question regarding 2D Sprite animations that I haven\'t been able to find specifically answered anywhere:

I have a sprite with walk animations to t

5条回答
  •  臣服心动
    2020-12-30 07:11

    This is my C# implementation. 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);
        }
    }
    

提交回复
热议问题