Unity 5: перевернуть игровой объект в определенном порядке

Я могу перевернуть игровой объект, но моя проблема в том, что он переворачивается до начала анимации атаки. Я не знаю, как привести это в порядок. Надеясь, что кто-то может помочь.

// Update is called once per frame
void Update () {

    if(Input.GetKeyDown(KeyCode.Space))
    {
        GetComponent<Rigidbody2D>().velocity = new Vector2(4f, 0);       
        StartCoroutine(enemyReturn());
    }
}

IEnumerator enemyReturn()
{
    yield return new WaitForSeconds(1.1f);
    GetComponent<Animator>().SetTrigger("slimeMelee"); //attack animation
    GetComponent<Rigidbody2D>().velocity = new Vector2(0, 0);
    Vector3 theScale = transform.localScale; theScale.x *= -1;
    transform.localScale = theScale;
}

person lu_laude    schedule 14.08.2017    source источник


Ответы (1)


Одним из тривиальных решений было бы уступить некоторое время перед переворачиванием:

IEnumerator enemyReturn()
{
    yield return new WaitForSeconds(1.1f);
    GetComponent<Animator>().SetTrigger("slimeMelee"); //attack animation
    yield return new WaitForSeconds(0.5f); // Setup this yield time.
    GetComponent<Rigidbody2D>().velocity = new Vector2(0, 0);
    Vector3 theScale = transform.localScale; theScale.x *= -1; transform.localScale = theScale; // code to flip
}

В этом примере код даст 0,5 секунды, прежде чем сделать флип. Вы должны изменить 0.5 на время, соответствующее анимации.

person lilezek    schedule 14.08.2017
comment
Спасибо за помощь - person lu_laude; 14.08.2017