Y
Y
Yuri2020-07-28 15:52:46
C++ / C#
Yuri, 2020-07-28 15:52:46

Damage animation problem, how to solve it?

Good day, can you please help to implement the damage animation when in contact with the enemy, here is my CollisionDamage script:

public class CollisionDamage : MonoBehaviour 
{
 public int damage =10;
 [SerializeField] private Animator animator;
 private Health health;
 [SerializeField] private SpriteRenderer spriteRenderer;
 private float direction;
 public float Direction
 
 {
   get{ return direction; }
 }
 private void OnCollisionEnter2D(Collision2D col) 
 {
        if (GameManager.Instance.collisionContainer.ContainsKey(col.gameObject)) 
    {
            health = GameManager.Instance.collisionContainer[col.gameObject];
            direction = (col.transform.position - transform.position).x;
            animator.SetFloat("Direction", Mathf.Abs(direction));		
        }
}
 public void SetDamage()
 {
   if (health!=null)
   health.TakeHit(damage);
   health =null;
   direction = 0;
   animator.SetFloat("Direction",0F);
 }
 	
}

How can I write a damage animation for a character in this script, the idea is this: there is contact, there is damage, there is no contact, and there is no damage.
PS this script is attached to the enemy.
The animation itself is played in the player in this place (Player script):
animator.SetBool("isGrounded",groundDetection.isGrounded);
    if(!isJumping && !groundDetection.isGrounded)
    {
      animator.SetTrigger("StartFall");
      animator.SetBool("Damaged",false);
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question