Y
Y
Yuri2020-07-04 01:30:11
Unity
Yuri, 2020-07-04 01:30:11

How to make the bite animation of the enemy play at the moment of collision with the player?

I have three animations at the enemy (rest, walk and bite), distributed as follows: 5effb06914f52953020265.jpeg
Below is my enemy patrol code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyPatrol : MonoBehaviour 
{
 public GameObject LeftBorder;  
 public GameObject RightBorder;
 public Rigidbody2D rigidBody;
 public bool isRightDirection;
 public float speed;
 public GroundDetection groundDetection;
 public Animator animator;
  void Update() 
  {
    if (isRightDirection && groundDetection.isGrounded) 
    {
 rigidBody.velocity= Vector2.right *speed;
    if(transform.position.x > RightBorder.transform.position.x)
 isRightDirection =!isRightDirection;
 
    }
    else if(groundDetection.isGrounded)
    {
 rigidBody.velocity= Vector2.left *speed;
 if(transform.position.x < LeftBorder.transform.position.x)
 isRightDirection =!isRightDirection;
 

  }
}

I would be glad for any advice and help on adding new lines to the script that would make the animations work correctly. That is, so that the animations play smoothly. Well, I would also like to hear if triggers are needed in this case? PS: damage is processed by the "Player" and "Enemy" tags.
My scene looks something like this:5effb13f4a493616753290.jpeg

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yuriy, 2020-07-12
@uriy99

Good day to all, thanks for the answers) I made my own version of animations and transitions. The code is below:

public Animator animator;
    public SpriteRenderer spriteRenderer;
    private bool biting;

    void Update() {
        animator.SetFloat("Speed", Mathf.Abs(rb.velocity.x) > 0 ? 1 : 0);
        spriteRenderer.flipX = isRightDirection;

        if (biting) 
        {
            return;
        }
...
void OnCollisionEnter2D(Collision2D collision) 
    {
        if (collision.gameObject.tag == "Player") {
            biting = true;
            animator.SetBool("Biting", true);
            isRightDirection = collision.transform.position.x > transform.position.x;
        }
    }
...

In the transitions, I removed the delay time and it's ready, the task is completed. With the speed I set Greatest 0.01 and Less 0.01. Something like that.

R
ReWire_92, 2020-07-05
@ReWire_92

I don’t quite understand, but now the bite animation is not playing smoothly or what? Judging by the animator, transitions from all states are configured, when the animation is triggered, smoothness should be realized by transitions from one state to another.
Are there 2D colliders on player and enemy objects? The easiest way is to transfer the bite animation to the animator when the colliders of the player and the enemy collide, and the animator, if there are configured transitions, will already ensure smoothness.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question