我有一个关于 2D Sprite 动画的快速问题,我无法在任何地方找到具体回答:
我有一个 Sprite ,右边有行走动画。但是,我显然想在他向左走时将动画向左翻转(2D 横向卷轴)。
我可以使用 transform.localscale.x
轻松翻转 sprite 本身,但是,这只会翻转 sprite。不是动画剪辑。 (这在 Unity 中不再发生)
因此,当 Sprite 翻转时,动画片段开始播放的那一刻,它会向右翻转(因为我唯一的动画片段是面向右侧的 Sprite )。
唯一的方法是在 Photoshop 中翻转 Sprite ,还是有办法在 Unity 中做到这一点?
谢谢!
更新:对于实际版本的 unity,如果您通过将变换乘以 -1
来缩放变换,动画帧也会缩放。
最佳答案
我终于通过这样做弄明白了:
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"));
}
关于c# - 在 Unity 2D 中翻转 2D Sprite 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26568542/