在 Unity 2D 中翻转 2D 精灵动画 [英] Flipping a 2D Sprite Animation in Unity 2D
问题描述
我有一个关于 2D Sprite 动画的快速问题,但在任何地方都找不到具体的答案:
I've got a quick question regarding 2D Sprite animations that I haven't been able to find specifically answered anywhere:
我有一个带有右侧行走动画的精灵.但是,当他向左走时,我显然想将动画向左翻转(2D 横向卷轴).
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).
我可以使用 transform.localscale.x
轻松翻转精灵本身,但是,这只翻转精灵.不是动画剪辑.(这在 Unity 中不再发生)
I can easily flip the sprite itself, using transform.localscale.x
, however, that only flips the sprite. Not the animation clip. (This no longer happens in 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).
这是在 Photoshop 中翻转精灵的唯一方法,还是有办法在 Unity 中做到这一点?
Is the only way to do this to flip the sprites in Photoshop, or is there a way to do it in Unity?
谢谢!
更新:如果您通过乘以 -1
来缩放变换,则使用 unity 的实际版本,动画帧也会缩放.
UPDATE: With the actual versions of unity if you scale the transform by multiplying it by -1
, the animation frames are also scaled.
推荐答案
我终于通过这样做找到了:
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;
}
这是来自 Unity 的 2D Platformer 示例.
This is from Unity's 2D Platformer example.
要使用 Flip
方法实现某种检查,您可以执行类似于以下示例的操作,该示例是基本移动代码.faceRight
设置为类上的一个值,以便其他方法可以使用它,默认为false
.
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. facingRight
is set as a value on the class so that the other methods can use it, and it is defaulted to 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"));
}
这篇关于在 Unity 2D 中翻转 2D 精灵动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!