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

Making each enemy unique

Discussion in 'Scripting' started by AyeJayy, Apr 30, 2020.

  1. AyeJayy

    AyeJayy

    Joined:
    Apr 19, 2020
    Posts:
    8
    I spent a whole day making some code to setup my battles and use stats for the player and the enemy. It works great and my game 's combat is now working. My problem is this, the script that runs my battle scene called, "battle system" asks for a prefab for each character that will appear on screen, and then I have to manually put in the numbers for each character. I want the player to walk into an enemy in an overworld which will switch the scene to the "Battle System". Should I make multiple prefabs and find a way to get my battle system code to be able to read the prefab on the fly or should I make multiple copies of the battle system script. That way it will house each unique prefab.
     
  2. sbalanoff

    sbalanoff

    Joined:
    Nov 14, 2019
    Posts:
    36
    could you reference your code?

    If you want the game to switch over to the battlefeild when you touch a player you would do something like the following:


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagment;
    3.  
    4. public class Enemy : MonoBehaviour
    5. {
    6.  
    7.     string battleField = "battle system";
    8.  
    9.     private void OnCollisionEnter(Collision collision)
    10.     {
    11.         if (collision.collider.CompareTag("enemy"))
    12.         {
    13.             SceneManager.LoadScene(battleField);
    14.         }
    15.     }
    16. }
     
  3. AyeJayy

    AyeJayy

    Joined:
    Apr 19, 2020
    Posts:
    8
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Diagnostics;
    4. using System.Globalization;
    5. using System.Threading;
    6. using UnityEngine;
    7. using UnityEngine.UI;
    8.  
    9. public enum BattleState { START, WON, LOST }
    10.  
    11. public class BattleSystem : MonoBehaviour
    12. {
    13.  
    14.     public GameObject playerPrefab;
    15.     public GameObject enemyPrefab;
    16.  
    17.     public Transform playerBattleStation;
    18.     public Transform enemyBattleStation;
    19.  
    20.     public Unit playerUnit;
    21.     public Unit enemyUnit;
    22.     //public Text dialogueText;
    23.  
    24.     public BattleHUD playerHUD;
    25.     public BattleHUD enemyHUD;
    26.     float time = 1;
    27.  
    28.     public BattleState state;
    29.     // Start is called before the first frame update
    30.  
    31.     void SetupBattle()
    32.     {
    33.         GameObject playerGO = Instantiate(playerPrefab, playerBattleStation);
    34.         playerUnit = playerGO.GetComponent<Unit>();
    35.  
    36.         GameObject enemyGO = Instantiate(enemyPrefab, enemyBattleStation);
    37.         enemyUnit = enemyGO.GetComponent<Unit>();
    38.  
    39.         //dialogueText.text = "A wild" + enemyUnit.unitName + "approaches..."
    40.         playerHUD.SetHUD(playerUnit);
    41.         enemyHUD.SetHUD(enemyUnit);
    42.  
    43.         StartCoroutine(EnemyAttack());
    44.     }
    45.  
    46.     void Start()
    47.     {
    48.         state = BattleState.START;
    49.         SetupBattle();
    50.         StartCoroutine(EnemyAttack());
    51.         time = 0;
    52.     }
    53.  
    54.     IEnumerator EnemyAttack()
    55.     {
    56.         while (time > (enemyUnit.Speed))
    57.         {
    58.              time = 0;
    59.  
    60.               playerUnit.TakeAttack(enemyUnit.Attack, playerUnit.Defense);
    61.  
    62.               playerHUD.SetHP(playerUnit.CurrentHP);
    63.  
    64.               yield return (new WaitForSeconds(enemyUnit.Speed));
    65.         }
    66.     }
    67.  
    68.     IEnumerator Punch()
    69.     {
    70.         enemyUnit.TakeAttack(playerUnit.Attack, enemyUnit.Defense);
    71.  
    72.         yield return new WaitForSeconds(.5f);
    73.  
    74.         //bool isDead = enemyUnit.TakeAttack(playerUnit.Attack, enemyUnit.Defense);
    75.  
    76.         enemyHUD.SetHP(enemyUnit.CurrentHP);
    77.  
    78.         //if (isDead)
    79.         {
    80.             //state = BattleState.WON;
    81.             //EndBattle();
    82.         }
    83.     }
    84.  
    85.     IEnumerator WaterWhip()
    86.     {
    87.         enemyUnit.WaterWhip(playerUnit.Attack);
    88.  
    89.         yield return new WaitForSeconds(.5f);
    90.  
    91.         enemyHUD.SetHP(enemyUnit.CurrentHP);
    92.     }
    93.  
    94.     IEnumerator FireBolt()
    95.     {
    96.         enemyUnit.FireBolt(playerUnit.Attack, enemyUnit.Defense);
    97.  
    98.         yield return new WaitForSeconds(.5f);
    99.  
    100.         enemyHUD.SetHP(enemyUnit.CurrentHP);
    101.         //tick
    102.         yield return new WaitForSeconds(5f);
    103.  
    104.         enemyUnit.HealthDrain(2);
    105.  
    106.         enemyHUD.SetHP(enemyUnit.CurrentHP);
    107.         //tick
    108.         yield return new WaitForSeconds(5f);
    109.  
    110.         enemyUnit.HealthDrain(2);
    111.  
    112.         enemyHUD.SetHP(enemyUnit.CurrentHP);
    113.         //tick
    114.         yield return new WaitForSeconds(5f);
    115.  
    116.         enemyUnit.HealthDrain(2);
    117.  
    118.         enemyHUD.SetHP(enemyUnit.CurrentHP);
    119.         //tick
    120.         yield return new WaitForSeconds(5f);
    121.  
    122.         enemyHUD.SetHP(enemyUnit.CurrentHP);
    123.     }
    124.  
    125.     IEnumerator PebbleBlast()
    126.     {
    127.         enemyUnit.PebbleBlast(playerUnit.Attack, enemyUnit.Defense);
    128.  
    129.         yield return new WaitForSeconds(.5f);
    130.  
    131.         //bool isDead = enemyUnit.TakeAttack(playerUnit.Attack, enemyUnit.Defense);
    132.  
    133.         enemyHUD.SetHP(enemyUnit.CurrentHP);
    134.  
    135.         playerUnit.ArmorUp(playerUnit.Defense);
    136.  
    137.         yield return new WaitForSeconds(5f);
    138.  
    139.         playerUnit.ArmorDown(playerUnit.Defense);
    140.  
    141.         //if (isDead)
    142.  
    143.         //state = BattleState.WON;
    144.         //EndBattle();
    145.  
    146.     }
    147.  
    148.     void EndBattle()
    149.     {
    150.         if (state == BattleState.WON)
    151.         {
    152.             //DialogueText.text = "Victory!";
    153.         }
    154.         else if (state == BattleState.LOST)
    155.         {
    156.             //dialogueText.text = "You have been defeated...";
    157.         }
    158.     }
    159.  
    160.     public void MPRecovery()
    161.     {
    162.         playerUnit.MPRecovery(1);
    163.  
    164.         playerHUD.SetMP(playerUnit.CurrentMP);
    165.  
    166.     }
    167.  
    168.     void Update()
    169.     {
    170.         {
    171.             time += Time.deltaTime;
    172.             StartCoroutine(EnemyAttack());
    173.         }
    174.    
    175.  
    176.         if (Input.GetKeyDown(KeyCode.W))
    177.         {
    178.             if (playerUnit.CurrentMP >= 5)
    179.                 StartCoroutine(PebbleBlast());
    180.             else return;
    181.             playerUnit.CurrentMP -= 5;
    182.             playerHUD.SetMP(playerUnit.CurrentMP);
    183.         }
    184.  
    185.         if (Input.GetKeyDown(KeyCode.D))
    186.         {
    187.             if (playerUnit.CurrentMP >= 5)
    188.                 StartCoroutine(WaterWhip());
    189.             else return;
    190.             playerUnit.CurrentMP -= 5;
    191.             playerHUD.SetMP(playerUnit.CurrentMP);
    192.         }
    193.  
    194.         if (Input.GetKeyDown(KeyCode.A))
    195.         {
    196.             if (playerUnit.CurrentMP >= 5)
    197.                 StartCoroutine(FireBolt());
    198.             else return;
    199.             playerUnit.CurrentMP -= 5;
    200.             playerHUD.SetMP(playerUnit.CurrentMP);
    201.         }
    202.  
    203.         if (Input.GetKeyDown(KeyCode.S))
    204.         {
    205.             if (playerUnit.CurrentMP >= 2)
    206.                 StartCoroutine(Punch());
    207.             else return;
    208.             playerUnit.CurrentMP -= 2;
    209.             playerHUD.SetMP(playerUnit.CurrentMP);
    210.         }
    211.     }
    212. }
     
  4. AyeJayy

    AyeJayy

    Joined:
    Apr 19, 2020
    Posts:
    8
    My issue is not spawning. This code requires that I select a single prefab and that's the prefab that will be pulled in Unity no matter what. I want this code to pull multiple prefabs.
     
  5. olejuer

    olejuer

    Joined:
    Dec 1, 2014
    Posts:
    210
    I don't really understand your question, to be honest. But if you want your script to reference a variable number of prefabs, you can make your field an array
    public GameObject[] enemyPrefabs;
    .
    If you want the enemies that are spawned to depend on the entity you encounter on the world map, then probably those entities on the world map should have a script that holds this array. They could then pass it to the BattleSystem when the battle starts.
     
    sbalanoff likes this.
  6. sbalanoff

    sbalanoff

    Joined:
    Nov 14, 2019
    Posts:
    36
    You could pull a random one with
    Code (CSharp):
    1. Instantiate(enemyPrefabs[Random.Range(0,enemyPrefabs.Length)], spawnPoint.position, Quaternian.identity);
     
  7. AyeJayy

    AyeJayy

    Joined:
    Apr 19, 2020
    Posts:
    8
    Thank you so much guys I just needed to turn the game object to an array!
     
    sbalanoff likes this.
  8. sbalanoff

    sbalanoff

    Joined:
    Nov 14, 2019
    Posts:
    36
    @AyeJayy if you wanted to ever remove a certain item from the list you would want to turn it into a List<T>.


    Code (CSharp):
    1. public List<GameObject> enemyPrefabs;
    2.  
    3. void Start(){
    4.     enemyPrefabs.Remove(enemyPrefabs.IndexOf(ObjectToRemove));
    5. }