Answer the question
In order to leave comments, you need to log in
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:
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;
}
}
Answer the question
In order to leave comments, you need to log in
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;
}
}
...
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 questionAsk a Question
731 491 924 answers to any question