Get player and enemy hit bars to respond to each others attack not their own
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 ( https://www.youtube.com/watch?annotation_id=annotation_146566169&feature=iv&src_vid=NgftVg3idB4&v=1lrkgdENfqM ) 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.
Answer by $$anonymous$$ · May 20, 2016 at 06:51 AM
I'm not sure I fully understand your code but in the OnTriggerEnter of the enemy you search if there is a player anywhere in your game and if true you say he is in range. But what if an enemy triggers OnTriggerEnter by another enemy or something else? Shouldn't you be checking if the other variable is the player?
Your answer
Follow this Question
Related Questions
How to create a child object inside the parent (script) and assign independent variables? 0 Answers
Adjust the size of the object within a parent container 0 Answers
Save/Load Player System 0 Answers
Player not moving in the right direction instantly 1 Answer
How do i ajust the player height acording to the height of the roof in a first person game? 1 Answer