I have made an up and down movement RPG prototype and I wanted to have a fighting system.
I made it so that the enemy follows the player and that I get damage every time he collides with me. The problem was that he only gave me damage once and would have to stop colliding with me, to attack again. I fixed that by changing OnCollisionEnter2D
to OnCollisionStay2D
, but now he attacks every frame, which makes him way too strong.
I already asked some people how to change it and they said you could do it with coroutines. The thing is that I have no idea how to code that and it gets even more complicated, because the enemy is constantly touching the player (When you're not dodging). I would be really happy if someone could help me with that
EnemyAttack.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyAttack : MonoBehaviour
{
[SerializeField] private GameObject Player;
[SerializeField] private int timer;
public int damage;
public PlayerHealth playerHealth;
public float AttackCooldown;
public float WaitForSecondsRealTime;
float PlayerHealthh;
private void OnCollisionStay2D(Collision2D collision) {
if(collision.gameObject.tag == "Player") {
playerHealth.TakeDamage(damage);
}
}
}