【问题标题】:Player keeps rotating when going left in Unity 2d在 Unity 2d 中向左移动时玩家一直在旋转
【发布时间】: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 剪辑吗?链接中的那个似乎不起作用

标签: c# unity3d


【解决方案1】:

当这行代码执行时,transform.Rotate(0,180,0); 习惯于 rotate 180 degree in y axis each time,这就是你的播放器不会停止旋转的原因

这是您可以使用的代码,它将阻止您的玩家根据方向旋转

 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);
                    this.transform.rotation = Quaternion.AngleAxis(180, Vector3.up);
                }
                else if (hDir > 0)  // Go right
                {
                    rb.velocity = new Vector2(speedX, rb.velocity.y);
                    this.transform.rotation = Quaternion.AngleAxis(0, Vector3.up);
                }
            }
        }
    }

【讨论】:

  • 你也可以为左边做transform.rotation = Quaternion.LookRotation(Vector3.back);,为右边做transform.rotation = Quaternion.identity;
【解决方案2】:

您需要添加一个 bool 变量来检查玩家是否面向右侧,以及一个翻转他的 Flip Function:

bool bFacingRight = true;

private Rigidbody2D rb;

int _switch;

private void FixedUpdate() {
    InputManager();
}

private void InputManager()
{
    if (state == State.hurt || anm.GetCurrentAnimatorStateInfo(0).IsTag("attack"))
       return;
    
    float hDir = Input.GetAxis("Horizontal");

    // Move the character by finding the target velocity
    rb.velocity = new Vector2(hDir * 10f, rb.velocity.y);

    if (hDir < 0 && bFacingRight) { // Flip Left
       _switch = 0;
       Flip(_switch);
    }
    else if (hDir > 0  && !bFacingRight) { // Flip Right
       _switch= 1;
       Flip(_switch);
    }
}

private void Flip(int swth)
{
    // Switch the way the player is labelled as facing.
    bFacingRight = !bFacingRight;

    // Multiply the player's x local scale by -1.
    switch(swth)
    {
       case 0:
       this.transform.rotation = Quaternion.AngleAxis(180, Vector3.up);
       break;
       case 1:
       this.transform.rotation = Quaternion.AngleAxis(0, Vector3.up);
       break;
    }
}

【讨论】:

  • 一旦玩家向左移动,他们将永远不会转回面向右侧。
猜你喜欢
  • 1970-01-01
  • 2017-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多