banner



How Do I Make My Animation Flip In Unity

Sprite Flipping in Unity for 2D Animations

//Easily control sprite management.

Chop-chop Change the direction a sprite is facing

Today's post is a quick one. Frequently when working in 2D games we want to be able to modify the direction a sprite is facing to requite us a more polished blitheness. As always Unity makes this very unproblematic for us.

If you await at some older sprite sheets on the cyberspace you volition meet that in the past information technology was common place to create a sprite canvas animations for every direction. Take this example:

Sample Sprite sheet from Pinterest (Pin on Animation Tutorial (pinterest.com)

In this example a left and correct movement makes sense, because it looks at the role player from the left and the right sides and his attire makes it more polished. Yet, in a game like the mobile run a risk I am working on nosotros only take a right facing sprite. Unity lets us take care of this directly in the Sprite Renderer.

The Renderer in the Inspector

Notice the Sprite Renderer has a setting called Flip with options for X and Y. This ways we tin can easily flip the sprite in code to deal with the transition from facing left to right. Let us have a look at the code it takes to accomplish this.

I have the sprite flip code inside my animation class (full code beneath) since it is part of the animation to me.

Outset we demand a handle to the sprite renderer in the script for like shooting fish in a barrel access. So we create a form variable:

          private SpriteRenderer _renderer;        

Adjacent, we need to initialize the new variable, and then we have care of that in Start()

          void            Start()
{
_renderer = GetComponent<SpriteRenderer>();
if (_renderer == cypher)
{
Debug.LogError("Player Sprite is missing a renderer");
}
}

Finally, in the Update() method we check the status of the players motility direction using the GetAxisRaw() on the the Horizontal input and flip the X axis based on the results.

          void            Update()
{
if (Input.GetAxisRaw("Horizontal") > 0)
{
_renderer.flipX = false;
}
else if (Input.GetAxisRaw("Horizontal") < 0)
{
_renderer.flipX = true;
}
}

That'south information technology. Now when we play the game our player will flip to the management he is facing.

It is important to notation that this does await a little clunky if you pay too much attending besides it. The player e'er has his sword in the hand closest to the screen in my sample, then its equally if he swaps his sword to the other hand when he flips. This might break the immersion for some players, simply when y'all have a sprite who is mostly identical on the left and correct sides (call back Mario or Sonic) the flip impact is almost unnoticeable.

Equally promised here is the current full code for the animation class.

          using UnityEngine;

public course Player_Animation_Contoller : MonoBehaviour
{
individual Animator _animator;
individual SpriteRenderer _renderer;
individual static readonly int MovementSpeed = Animator.StringToHash("MovementSpeed");

// Start is called before the first frame update
void Start()
{
_animator = GetComponent<Animator>();
if (_animator == naught)
{
Debug.LogError("The role player sprite is missing an Animator Component");
}

_renderer = GetComponent<SpriteRenderer>();
if (_renderer == zip)
{
Debug.LogError("Histrion Sprite is missing a renderer");
}
}

// Update is called in one case per frame
void Update()
{
if (Input.GetAxisRaw("Horizontal") > 0)
{
_renderer.flipX = fake;
}
else if (Input.GetAxisRaw("Horizontal") < 0)
{
_renderer.flipX = true;
}
_animator.SetFloat(MovementSpeed, Mathf.Abs(Input.GetAxisRaw("Horizontal")));
}
}

Source: https://levelup.gitconnected.com/sprite-flipping-in-unity-for-2d-animations-f5c33d3e8f71

Posted by: pridgenforome.blogspot.com

0 Response to "How Do I Make My Animation Flip In Unity"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel