Анимация не переключается - PullRequest
0 голосов
/ 01 ноября 2019

У меня много анимаций в моей 2d игре на единство. Все они работают хорошо, потому что я снимаю флажок «Имеет время выхода» и делаю «Продолжительность транзита» между ними равным 0. Но у меня проблема с анимацией атаки. Это не петля. Если я установлю «Транзитная длительность» на 0, то это не закончится, только если я нажму на что-нибудь. Поэтому я установил «Длительность транзита» до 0,3, но теперь он не работает сразу по клику, как мне нужно.

    Rigidbody2D rb;
    Animator anim;
    float speed = 6f;
    public int jump;
    private bool IsGrounded;
    public Transform groundCheck;
    public LayerMask whatIsGround;


    private void Start() {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }

    private void Update() {
        IsGrounded = Physics2D.OverlapCircle(groundCheck.position, 0.5f, whatIsGround); //Is player on ground
        if (IsGrounded && rb.velocity.y > -0.01f && rb.velocity.y < 0.01f) {
            jump = 0;
            if (anim.GetCurrentAnimatorStateInfo(0).IsName("Attack_1") == false) {
                anim.SetInteger("State", 0);
            }
        }
        if (jump == 3) {
            jump = 1;
        }
        if (Input.GetAxis("Horizontal") < 0) { //Running left
            GetComponent<SpriteRenderer>().flipX = true;
            if (IsGrounded && rb.velocity.y > -0.01f && rb.velocity.y < 0.01f) {
                anim.SetInteger("State", 1);
            }
        }
        if (Input.GetAxis("Horizontal") > 0) { //Running right
            GetComponent<SpriteRenderer>().flipX = false;
            if (IsGrounded && rb.velocity.y > -0.01f && rb.velocity.y < 0.01f) {
                anim.SetInteger("State", 1);
            }
        }
        if (rb.velocity.y > 0.01f) { //Animation of falling
            anim.SetInteger("State", 2);
        }
        else if (rb.velocity.y < -0.01f) { //Animation of jumping
            anim.SetInteger("State", 3);
        }
        if (Input.GetKeyDown(KeyCode.UpArrow) && (IsGrounded || jump < 2)) {
            jump += 1;
            if (jump == 1 || jump == 3) {
                anim.SetInteger("State", 5); //Another jumping animation
            }
            rb.velocity = Vector2.up * 12f;
        }
        if (Input.GetKeyDown(KeyCode.Z)) { //Attack animation
            anim.SetInteger("State", 4);
        }
}
```[![My animator][1]][1]


  [1]: https://i.stack.imgur.com/GegLW.png

Как я могу заставить его работать сразу?

Ответы [ 2 ]

0 голосов
/ 02 ноября 2019

Кажется, я решил свою проблему: я просто использовал Trigger для входа в атаку, а не "State" int.

Мой код сейчас:

Rigidbody2D rb;
    Animator anim;
    float speed = 6f;
    public int jump;
    private bool IsGrounded;
    public Transform groundCheck;
    public LayerMask whatIsGround;


    private void Start() {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }

    private void Update() {
        IsGrounded = Physics2D.OverlapCircle(groundCheck.position, 0.5f, whatIsGround); //
        if (IsGrounded && rb.velocity.y > -0.01f && rb.velocity.y < 0.01f) {
            jump = 0;
            anim.SetInteger("State", 0);
        }
        if (jump == 3) {
            jump = 1;
        }
        if (Input.GetAxis("Horizontal") < 0) {
            GetComponent<SpriteRenderer>().flipX = true;
            if (IsGrounded && rb.velocity.y > -0.01f && rb.velocity.y < 0.01f && anim.GetCurrentAnimatorStateInfo(0).IsName("Attack_1") == false) {
                anim.SetInteger("State", 1);
            }
        }
        if (Input.GetAxis("Horizontal") > 0) {
            GetComponent<SpriteRenderer>().flipX = false;
            if (IsGrounded && rb.velocity.y > -0.01f && rb.velocity.y < 0.01f && anim.GetCurrentAnimatorStateInfo(0).IsName("Attack_1") == false) {
                anim.SetInteger("State", 1);
            }
        }
        if (rb.velocity.y > 0.01f) {
            anim.SetInteger("State", 2);
        }
        else if (rb.velocity.y < -0.01f) {
            anim.SetInteger("State", 3);
        }
        if (Input.GetKeyDown(KeyCode.UpArrow) && (IsGrounded || jump < 2)) {
            jump += 1;
            if (jump == 1 || jump == 3) {
                anim.SetInteger("State", 5);
            }
            rb.velocity = Vector2.up * 12f;
        }
        if (Input.GetKeyDown(KeyCode.Z)) {
            anim.SetTrigger("Attack");
        }
}
0 голосов
/ 01 ноября 2019

Снятие флажка «Имеет время выхода» позволяет прерывать анимацию друг друга.

Если ваша анимация атаки должна закончиться, прежде чем она сможет перейти к другой анимации, вы должны установить флажок «Имеет время выхода» на true. Вы можете оставить длительность перехода равной 0.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...