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:
- Player gets in contact with enemy
- Player deals damage
- Check if enemy is still alive, then go to 4.
- Enemy deals damage
- 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)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum BattleState { NOBATTLE, PLAYERTURN, ENEMYTURN, WON, LOST }
public class BattleSystem : MonoBehaviour
{
public PlayerAttributesManager playerAtm;
public EnemyAttributesManager enemyAtm;
public BattleState State;
void Start()
{
Debug.Log("Battle Script Working!");
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Enemy")) {
float enemyHP = other.gameObject.GetComponent<EnemyAttributesManager>().EnemyHP;
Debug.Log("Collided with enemy");
StartCoroutine(PlayerTurn());
}
}
// PLAYER TURN
IEnumerator PlayerTurn()
{
State = BattleState.PLAYERTURN;
{
playerAtm.DealDamage(GetComponent<EnemyAttributesManager>().EnemyHP);
}
Debug.Log("Waiting...");
yield return new WaitForSeconds (1);
Debug.Log("Done waiting");
if(gameObject.GetComponent<EnemyAttributesManager>().EnemyHP <= 0)
{
BattleWon();
}
else
{
StartCoroutine(EnemyTurn());
}
}
// ENEMY TURN
IEnumerator EnemyTurn()
{
State = BattleState.ENEMYTURN;
{
enemyAtm.EnemyDealsDamage(playerAtm.gameObject);
}
Debug.Log("Waiting...");
yield return new WaitForSeconds (1);
Debug.Log("Done waiting");
if(playerAtm.HP <= 0)
{
BattleLost();
}
else
{
StartCoroutine(PlayerTurn());
}
}
// BATTLE WON
void BattleWon()
{
State = BattleState.WON;
Debug.Log("Battle Won");
State = BattleState.NOBATTLE;
}
// BATTLE LOST
void BattleLost()
{
State = BattleState.LOST;
Debug.Log("Battle Lost");
}
}
EnemyAttributesManager Script (Every enemy has this script attached to it)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyAttributesManager : MonoBehaviour
{
//Enemy Attributes
public float EnemyHP;
public float EnemyATK;
public float EnemyDEF;
public float EnemyGold;
public float EnemyEXP;
public float EnemyFireProt;
public float EnemyIceProt;
public float EnemyLightningProt;
public void EnemyTakesDamage(float amount)
{
EnemyHP -= amount - EnemyDEF;
}
public void EnemyDealsDamage(GameObject target)
{
var atm = target.GetComponent<PlayerAttributesManager>();
if(atm != null)
{
atm.TakeDamage(EnemyATK);
}
}
}
PlayerAttributesManager Script (The player has this script attached to it)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAttributesManager : MonoBehaviour
{
//Player Attributes
public float HP;
public float ATK;
public float DEF;
public float Gold;
public float EXP;
public float Luck;
public float CriticalChance;
public float FireProt;
public float IceProt;
public float LightningProt;
public void TakeDamage(float amount)
{
HP -= amount - DEF;
}
public void DealDamage(GameObject target)
{
var atm = target.GetComponent<EnemyAttributesManager>();
if(atm != null)
{
atm.EnemyTakesDamage(ATK);
}
}
}