C # UNITY 2D пытается перевернуть спрайт в направлении х - PullRequest
0 голосов
/ 19 ноября 2018

Все, что я пытаюсь сделать, это когда враг поворачивается вокруг, его спрайт переворачивается, код ниже - это то, что мне удалось, но сейчас он переворачивается только после двух изменений в направлении, которое близко, но не то, что мне нужно. Пожалуйста, помогите.

   [HideInInspector]
    public bool facingRight = true;
    public float speed;
    Rigidbody2D rbody;
    public float timer;
    Animator anim;
    public float directionx;

// Use this for initialization

void Start()
    {
        rbody = GetComponent<Rigidbody2D>();

        StartCoroutine(Move(timer));
    }



    IEnumerator Move(float timer)
    {
        while (0 < 1)

        {
            Vector2 targetVelocity = new Vector2(directionx, 0);

            rbody.velocity = targetVelocity * speed;

            yield return new WaitForSeconds(timer);

            facingRight = !facingRight;

            Vector3 theScale = transform.localScale;

            theScale.x *= -1;

            transform.localScale = theScale;

            Vector2 targetVelocity1 = new Vector2(-directionx, 0);

            rbody.velocity = targetVelocity1 * speed;

            yield return new WaitForSeconds(timer);
        }

    }

}

1 Ответ

0 голосов
/ 19 ноября 2018

Код довольно понятен, вам нужно установить только одну переменную, чтобы узнать направление, в котором игрок начинает двигаться (m_FacingRight в моем случае), а затем решить, когда вы хотите, чтобы произошел «бросок», вызывая flipFunction() * * 1003

private bool m_FacingRight = true;  // For determining which way the player is currently facing.  

private void flipFunction()
{
  // Switch the way the player is labelled as facing.
  m_FacingRight = !m_FacingRight;

  // Multiply the player's x local scale by -1.
  Vector3 theScale = transform.localScale;
  theScale.x *= -1;
  transform.localScale = theScale;    
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...