I’m working on a 3d top-down melee fighting game (I’m quite new to Unity and programming, it’s my second project). In order to make fighting system I used this tutorial Melee Combat & Weapon System - Unity Beginner Tutorial - YouTube (Of course I changed some things to adjust it to top-down game. I had some problems with collision of attacks, while rotating character in-game I could attack single enemy multiple times during single animation. So I made bool which would check if enemy is hit and reset itself after some time. It worked well with single clone, however it doesn’t allow me to hit multiple clones (I want to make a wide slash, which could hit multiple enemies, but everyone could be damaged just once). I think that the problem is that every clone has same script that applies to all of them, so if one of them is hit, than it applies being hit to all of them. Is there any way to have these clones individual scripts of checking if they are hit? Here are my codes.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CollisionDetection : MonoBehaviour
{
public WeaponController wc;
public GameObject HitParticle;
EnemyHealth enemyHealth;
public GameObject Enemy;
void Awake()
{
enemyHealth = Enemy.GetComponent<EnemyHealth>();
}
private void OnTriggerEnter(Collider other)
{
if(other.tag == "Enemy" && wc.IsAttacking && enemyHealth.EnemyAlreadyHit == false)
{
Debug.Log(other.name);
Instantiate(HitParticle, new Vector3(other.transform.position.x, transform.position.y, other.transform.position.z), other.transform.rotation);
enemyHealth.EnemyAlreadyHit = true;
StartCoroutine(enemyHealth.EnemyAlreadyHitReset());
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyHealth : MonoBehaviour
{
public bool EnemyAlreadyHit = false;
public IEnumerator EnemyAlreadyHitReset()
{
yield return new WaitForSeconds(0.8f);
EnemyAlreadyHit = false;
}
}