Hello, I have got halfway though my project game but I have came across the issue of making the player attack enemies but hit the right Health-bars for different enemies, at the moment everything in my game hits itself and I think it has something to do with my health.CurrentVal, I have been trying to understand this for days now and this is kinda a last resort.
I started by following this tutorial ( 1. Unity 5 health bar tutorial - Intro - YouTube ) and I have kind of adapted these to suit my needs.
I think the problem is with this script (AttackBaddie and AttackPlayer)
the player has (AttackBaddie)
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class AttackBaddie : MonoBehaviour
{
[SerializeField]
public Stat health;
GameObject player;
bool BaddieInRange;
float timer;
public void Start()
{
timer = 1f;
}
public void Awake()
{
health.Initialize();
}
void OnTriggerEnter (Collider other)
{
if (other.transform.tag == "baddie")
{
BaddieInRange = true;
Debug.Log("In Range");
}
}
void OnTriggerExit (Collider other)
{
if (other.transform.tag == "baddie")
{
BaddieInRange = false;
Debug.Log("NOT In Range");
}
}
void Update()
{
timer += Time.deltaTime;
if (BaddieInRange == true && (Input.GetKeyDown("space")))
{
Debug.Log("Space is HERE!");
Attack();
}
}
public void Attack()
{
if (timer >= 2f)
{
//GameObject.FindWithTag("baddie").GetComponent<AttackPlayer>().health.CurrentVal -= 10;
health.CurrentVal -= 10;
Debug.Log("OW This Hurts! Baddie 1");
// Reset the timer.
timer = 0f;
}
if (health.CurrentVal <= 5)
{
Destroy (GameObject.FindWithTag("baddie"));
}
}
}
and the enemy has (AttackPlayer)
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class AttackPlayer : MonoBehaviour {
[SerializeField]
public Stat health;
public float timeBetweenAttacks = 1f;
GameObject baddie;
bool playerInRange;
float timer;
public void Start(){
timer = 2f;
}
public void Awake()
{
health.Initialize();
}
void OnTriggerEnter (Collider other)
{
if (GameObject.FindWithTag("Player"))
{
playerInRange = true;
}
}
void OnTriggerExit (Collider other)
{
if (GameObject.FindWithTag("Player"))
{
playerInRange = false;
}
}
void Update ()
{
timer += Time.deltaTime;
if(timer >= timeBetweenAttacks && playerInRange == true)
{
Attack ();
}
}
public void Attack ()
{
if(timer >= 4f) //
{
//I think the problem is here
health.CurrentVal -= 10;
timer = 0f;
}
}
}
Could someone point me in the right direction to solve it.