【发布时间】:2020-06-26 07:14:26
【问题描述】:
我正在使用 Unity 在 2D 平台游戏中移动我的玩家精灵,如果按下右键,它将向右移动,如果按下左键,它将向左移动。
向右移动工作正常,但是每当玩家向左移动时,我将 Y 旋转更改为 180,并且玩家应该只显示左侧动画,但向左时它会一直来回旋转。
请参阅video sample。
这是我的代码:
private Rigidbody2D rb;
private void FixedUpdate() {
InputManager();
}
private void InputManager()
{
float hDir = Input.GetAxis("Horizontal");
if (state != State.hurt) {
if(!anm.GetCurrentAnimatorStateInfo(0).IsTag("attack"))
{
if (hDir < 0) // Go left
{
rb.velocity = new Vector2(-speedX, rb.velocity.y);
transform.Rotate(0f, 180f, 0f);
}
else if (hDir > 0) // Go right
{
rb.velocity = new Vector2(speedX, rb.velocity.y);
transform.Rotate(0f, 0f, 0f);
}
}
}
}
如何让我的播放器在向左移动时坚持左侧动画?请不要建议更改localScale,因为我知道它有效,但出于射击目的,我的播放器最好轮换。
【问题讨论】:
-
相当肯定
rotate将当前变换旋转给定的四元数 - 所以你只是一遍又一遍地旋转播放器。你可能想直接设置transform.rotation。 -
啊,这显然不是真的.. 有趣的是,因为你的代码看起来不错,你有这个问题的 gif/video 剪辑吗?链接中的那个似乎不起作用