Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Simple automatic turn-based combat on collision not working

Discussion in 'Scripting' started by robinves17, Dec 5, 2022.

  1. robinves17

    robinves17

    Joined:
    Nov 22, 2022
    Posts:
    2
    Hi!

    So I'm developing my first game and am currently working into my first problem that I really cannot seem to fix by myself, even having gone through multiple tutorials. I'm probably making some stupid mistakes however so I really hope someone could provide me with some help.

    Basically what I'm trying to achieve is as follows:

    1. Player gets in contact with enemy
    2. Player deals damage
    3. Check if enemy is still alive, then go to 4.
    4. Enemy deals damage
    5. Check if player is still alive, then go to 2.
    etc etc

    I had everything working, but ONLY when I set the specific player attributes script and the specific enemy attributes script (Where the Health, attack, defense vars are and the scripts for Deal and TakeDamage)

    But I want it to take information from whichever enemy I come in contact with.
    Since I'm new to coding on this level I'm just trying things but I'm not 100% sure what I'm doing, maybe it's best if I share the scripts here:


    The BattleSystem Script: (Please note, I realise it isn't correct the way it is, but I just didn't know how to proceed after the OnTriggerEnter2D part of the script)
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6.  
    7. public enum BattleState { NOBATTLE, PLAYERTURN, ENEMYTURN, WON, LOST }
    8.  
    9. public class BattleSystem : MonoBehaviour
    10. {
    11.     public PlayerAttributesManager playerAtm;
    12.     public EnemyAttributesManager enemyAtm;
    13.     public BattleState State;
    14.    
    15.  
    16.  
    17.   void Start()
    18.   {
    19.           Debug.Log("Battle Script Working!");
    20.          
    21.   }
    22.    
    23.  
    24.  
    25.      private void OnTriggerEnter2D(Collider2D other)
    26.    
    27.     {
    28.         if (other.gameObject.CompareTag("Enemy")) {
    29.             float enemyHP = other.gameObject.GetComponent<EnemyAttributesManager>().EnemyHP;  
    30.  
    31.             Debug.Log("Collided with enemy");
    32.  
    33.             StartCoroutine(PlayerTurn());
    34.            
    35.         }
    36.     }
    37.  
    38. // PLAYER TURN
    39.  
    40.     IEnumerator PlayerTurn()
    41.    
    42.     {
    43.        
    44.         State = BattleState.PLAYERTURN;
    45.  
    46.        
    47.         {
    48.             playerAtm.DealDamage(GetComponent<EnemyAttributesManager>().EnemyHP);
    49.         }
    50.         Debug.Log("Waiting...");
    51.         yield return new WaitForSeconds (1);
    52.         Debug.Log("Done waiting");
    53.             if(gameObject.GetComponent<EnemyAttributesManager>().EnemyHP <= 0)
    54.                 {
    55.                 BattleWon();
    56.                 }
    57.             else
    58.                 {
    59.                 StartCoroutine(EnemyTurn());
    60.                 }          
    61.     }
    62.  
    63. // ENEMY TURN
    64.  
    65.     IEnumerator EnemyTurn()
    66.    
    67.     {  
    68.         State = BattleState.ENEMYTURN;
    69.        
    70.         {
    71.             enemyAtm.EnemyDealsDamage(playerAtm.gameObject);
    72.         }
    73.         Debug.Log("Waiting...");
    74.         yield return new WaitForSeconds (1);
    75.         Debug.Log("Done waiting");
    76.             if(playerAtm.HP <= 0)
    77.                 {
    78.                 BattleLost();
    79.                 }
    80.             else
    81.                 {
    82.                 StartCoroutine(PlayerTurn());
    83.                 }              
    84.     }
    85.  
    86. // BATTLE WON
    87.     void BattleWon()
    88.  
    89.     {
    90.         State = BattleState.WON;
    91.         Debug.Log("Battle Won");
    92.         State = BattleState.NOBATTLE;
    93.     }
    94.  
    95. // BATTLE LOST
    96.     void BattleLost()
    97.     {
    98.         State = BattleState.LOST;
    99.         Debug.Log("Battle Lost");
    100.     }
    101.  
    102. }
    103.  
    104.  

    EnemyAttributesManager Script (Every enemy has this script attached to it)
    Code (CSharp):
    1. using System.Collections;
    2.  
    3. using System.Collections.Generic;
    4.  
    5. using UnityEngine;
    6.  
    7.  
    8.  
    9. public class EnemyAttributesManager : MonoBehaviour
    10.  
    11. {
    12.  
    13.  
    14.  
    15.     //Enemy Attributes
    16.  
    17.     public float EnemyHP;
    18.  
    19.     public float EnemyATK;
    20.  
    21.     public float EnemyDEF;
    22.  
    23.     public float EnemyGold;
    24.  
    25.     public float EnemyEXP;
    26.  
    27.     public float EnemyFireProt;
    28.  
    29.     public float EnemyIceProt;
    30.  
    31.     public float EnemyLightningProt;
    32.  
    33.  
    34.  
    35.     public void EnemyTakesDamage(float amount)
    36.  
    37.     {
    38.  
    39.         EnemyHP -= amount - EnemyDEF;
    40.  
    41.     }
    42.  
    43.  
    44.  
    45.  
    46.  
    47.     public void EnemyDealsDamage(GameObject target)
    48.  
    49.     {
    50.  
    51.         var atm = target.GetComponent<PlayerAttributesManager>();
    52.  
    53.         if(atm != null)
    54.  
    55.         {
    56.  
    57.             atm.TakeDamage(EnemyATK);
    58.  
    59.         }
    60.  
    61.     }
    62.  
    63. }
    64.  

    PlayerAttributesManager Script (The player has this script attached to it)
    Code (CSharp):
    1. using System.Collections;
    2.  
    3. using System.Collections.Generic;
    4.  
    5. using UnityEngine;
    6.  
    7.  
    8.  
    9. public class PlayerAttributesManager : MonoBehaviour
    10.  
    11. {
    12.  
    13.  
    14.  
    15.     //Player Attributes
    16.  
    17.     public float HP;
    18.  
    19.     public float ATK;
    20.  
    21.     public float DEF;
    22.  
    23.     public float Gold;
    24.  
    25.     public float EXP;
    26.  
    27.     public float Luck;
    28.  
    29.     public float CriticalChance;
    30.  
    31.     public float FireProt;
    32.  
    33.     public float IceProt;
    34.  
    35.     public float LightningProt;
    36.  
    37.  
    38.  
    39.     public void TakeDamage(float amount)
    40.  
    41.     {
    42.  
    43.         HP -= amount - DEF;
    44.  
    45.     }
    46.  
    47.  
    48.  
    49.  
    50.  
    51.     public void DealDamage(GameObject target)
    52.  
    53.     {
    54.  
    55.         var atm = target.GetComponent<EnemyAttributesManager>();
    56.  
    57.         if(atm != null)
    58.  
    59.         {
    60.  
    61.             atm.EnemyTakesDamage(ATK);
    62.  
    63.         }
    64.  
    65.     }
    66.  
    67.  
    68.  
    69.  
    70.  
    71. }
    72.  
     
  2. robinves17

    robinves17

    Joined:
    Nov 22, 2022
    Posts:
    2
    I think I finally did it. Will have to do some thorough testing later. Bottom line is that I had to include everything inside the void OnTriggerEnter2D(Collider2D other) script.

    New Battlesystem script now looks like this:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public enum BattleState { NOBATTLE, PLAYERTURN, ENEMYTURN, WON, LOST }
    7.  
    8. public class BattleSystem : MonoBehaviour
    9. {
    10.     public PlayerAttributesManager playerAtm;
    11.     public EnemyAttributesManager enemyAtm;
    12.     public BattleState State;
    13.    
    14.  
    15.  
    16.   void Start()
    17.   {
    18.           Debug.Log("Battle Script Working!");
    19.          
    20.   }
    21.    
    22.  
    23.  
    24.      private void OnTriggerEnter2D(Collider2D other)
    25.    
    26.     {
    27.         if (other.gameObject.CompareTag("Enemy"))
    28.         {  
    29.  
    30.             Debug.Log("Collided with enemy");
    31.  
    32.             StartCoroutine(PlayerTurn());
    33.         }
    34.    
    35.  
    36. // PLAYER TURN
    37.  
    38.     IEnumerator PlayerTurn()
    39.    
    40.     {
    41.        
    42.         State = BattleState.PLAYERTURN;
    43.  
    44.        
    45.         {
    46.             playerAtm.DealDamage(other.gameObject);
    47.         }
    48.         Debug.Log("Waiting...");
    49.         yield return new WaitForSeconds (1);
    50.         Debug.Log("Done waiting");
    51.             if(other.gameObject.GetComponent<EnemyAttributesManager>().EnemyHP <= 0)
    52.                 {
    53.                 BattleWon();
    54.                 }
    55.             else
    56.                 {
    57.                 StartCoroutine(EnemyTurn());
    58.                 }          
    59.     }
    60.  
    61.  
    62.         // ENEMY TURN
    63.  
    64.         IEnumerator EnemyTurn()
    65.    
    66.     {  
    67.         State = BattleState.ENEMYTURN;
    68.        
    69.         {
    70.             other.gameObject.GetComponent<EnemyAttributesManager>().EnemyDealsDamage(gameObject);
    71.         }
    72.         Debug.Log("Waiting...");
    73.         yield return new WaitForSeconds (1);
    74.         Debug.Log("Done waiting");
    75.             if(playerAtm.HP <= 0)
    76.                 {
    77.                 BattleLost();
    78.                 }
    79.             else
    80.                 {
    81.                 StartCoroutine(PlayerTurn());
    82.                 }              
    83.     }
    84.  
    85. // BATTLE WON
    86.     void BattleWon()
    87.  
    88.     {
    89.         State = BattleState.WON;
    90.         Debug.Log("Battle Won");
    91.         State = BattleState.NOBATTLE;
    92.     }
    93.  
    94. // BATTLE LOST
    95.     void BattleLost()
    96.     {
    97.         State = BattleState.LOST;
    98.         Debug.Log("Battle Lost");
    99.     }
    100.     }
    101. }
    102.