- Home /
 
Need help with my melee system and arrays
Hey guys,
I'm still fairly new to programming and this problem has been giving me a major headache. Hopefully one of you guys can help me out.
So I currently have a melee system working (using this guy's tutorials:https://www.youtube.com/watch?v=fxSM_INrmF8). The problem is that the weapon only focuses on one enemy of the same tag -- meaning it only inflicts damage to one instance of that enemy. Instead, I want it to do damage to multiple enemies of different tags (at most 3 tags) if they're in front of the player.
I know I'm going to have to implement a list, or array, to hold my enemies, but I'm not sure how. Any help would be great, thanks!
public class Melee : MonoBehaviour {
 public float lightAtkDmg, heavyAtkDmg; 
 public GameObject target;
 private float heavyDistance;
 private float lightDistance;
 private float deflectionDistance; 
 bool isAttacking = false; 
 // Use this for initialization
 void Start () {
      
     //target = 
 
 }
 
 // Update is called once per frame
 void Update () {
     Debug.DrawLine (target.transform.position, transform.position, Color.yellow);
 }
 public void heavyAttack(){
     isAttacking = true; 
     heavyDistance = Vector3.Distance (target.transform.position, transform.position); 
     Vector3 Dir = (target.transform.position - transform.position).normalized; 
     float Direction = Vector3.Dot (Dir, transform.forward); 
     Debug.Log (Direction); 
     Debug.Log (heavyDistance); 
     
     if (target.gameObject.tag == "Enemy" && heavyDistance < 3) {
         if(Direction>0){
             Debug.Log ("Hit Enemy_Charger w/Heavy");
             target.GetComponent<Entity> ().TakeDamage (heavyAtkDmg);
         }
     }
     
     if (target.gameObject.tag == "Destructible" && heavyDistance < 2) {
         if(Direction>0){
             Debug.Log ("Hit EDestructible w/Heavy");
             target.GetComponent<Entity> ().TakeDamage (heavyAtkDmg);
         }
     }
     if (target.gameObject.tag == "Magic" && heavyDistance < 2) {
         if (Direction >0 & heavyDistance >1.8){
             Debug.Log("magic deflected w/heavy");
         }
     } 
     isAttacking = false; 
 }
 public void lightAttack(){
     Debug.Log ("perform Light Attack");
     isAttacking = true; 
     Vector3 Dir = (target.transform.position - transform.position).normalized;
     float Direction = Vector3.Dot (Dir, transform.forward);
     lightDistance = Vector3.Distance (target.transform.position, transform.position); 
     Debug.Log ("Light Distance" + lightDistance); 
     //Debug.Log (Direction); 
     if (target.gameObject.tag == "Enemy" && lightDistance < 1) {
         if (Direction > 0){
             
             Debug.Log ("Hit Enemy w/light");
             target.GetComponent<Entity> ().TakeDamage (lightAtkDmg);
         }
     } 
     if (target.gameObject.tag == "Destructible" && lightDistance < 1) {
         if (Direction > 0){
             
             Debug.Log ("Hit Destructible w/light");
             target.GetComponent<Entity> ().TakeDamage (lightAtkDmg);
         }
     } 
     if (target.gameObject.tag == "Magic" && lightDistance < 15 && lightDistance > 1) {
         if (Direction > 0) {
             Debug.Log ("magic deflected w/light");
         }
     }
     isAttacking = false; 
 } 
 
 
               }
Your answer
 
             Follow this Question
Related Questions
How to Implement a “Parry” mechanic to a 2D game? 1 Answer
How to do bullet spread in 2D? 1 Answer
2D 360 Shooting Is not working (Top Down, Shooting in all directions) 1 Answer
How do I move camera towards the mouse while anchoring it to the player? 0 Answers
How to create a cinemachine custom camera offset in the aiming direction 0 Answers