How can I make my melee attack hit multiple prefab enemies?
I have my game set up and it seems to work fine when I melee combat the enemy I created. It's takes 3 hits and dies like I would expect.
I can't seem to damage the prefabs that I spawn on the level.
I can duplicate the Enemy gameObject, but then only the most recent duplicate will take damage and be affected by animation controllers.
I think it has something to do with my AttackTrigger c# script not targeting multiple enemies?
Here are my scripts
"Player Attack Script:"
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class PlayerAttack : MonoBehaviour {
private bool attacking = false;
private float attackTimer = 0;
private float attackCooldown = 0.3f;
public Collider attackTrigger;
private Animator anim;
void Awake (){
anim = gameObject.GetComponent<Animator> ();
attackTrigger.enabled = false;
}
void Update (){
Attack ();
}
void Attack () {
bool isAttacking = CrossPlatformInputManager.GetButton ("Attack");
if ((isAttacking) && !attacking)
{
attacking = true;
attackTimer = attackCooldown;
attackTrigger.enabled = true;
}
if (attacking)
{
if (attackTimer > 0)
{
attackTimer -= Time.deltaTime;
}
else
{
attacking = false;
attackTrigger.enabled = false;
}
}
anim.SetBool("isAttacking", attacking);
}
}
"Player Attack Trigger Script:"
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AttackTrigger : MonoBehaviour {
public int attackDamage = 10;
GameObject enemy;
GameObject enemies;
PlayerHealth playerHealth; // Reference to the player's health.
EnemyHealth enemyHealth; // Reference to this enemy's health.
bool enemyInRange; // Whether player is within the trigger collider and can be attacked.
float timer; // Timer for counting up to the next attack.
void Awake ()
{
enemy = GameObject.FindGameObjectWithTag ("Enemy");
enemies = GameObject.FindGameObjectWithTag ("Enemy");
enemyHealth = enemy.GetComponent<EnemyHealth> ();
}
void OnTriggerEnter (Collider other)
{
if(other.gameObject == enemy)
{
//Debug.Log ("I Touched " + other.name);
enemyHealth.TakeDamage (attackDamage);
if(enemyHealth.currentHealth > 0)
{
enemyHealth.TakeDamage (attackDamage);
}
}
}
}
"EnemyHealth Script:"
using UnityEngine;
public class EnemyHealth : MonoBehaviour
{
public int startingHealth = 30;
public int currentHealth;
public int scoreValue = 10;
bool damaged; // True when the player gets damaged.
bool isDead;
Animator anim;
private float timer = 0;
private float timerCooldown = 0.2f;
void Awake ()
{
anim = GetComponent <Animator> ();
bool isDead = false;
currentHealth = startingHealth;
}
void Update ()
{
timer += Time.deltaTime;
if (damaged)
{
anim.SetBool ("isHit", true);
// timer = 0;
}
if (timer >= timerCooldown)
{
anim.SetBool ("isHit", false);
}
}
public void TakeDamage (int amount)
{
damaged = true;
currentHealth -= amount;
if(currentHealth <= 0 && !isDead)
{
Death ();
}
timer = 0;
}
void Death ()
{
isDead = true;
ScoreManager.score += scoreValue;
Destroy(gameObject);
}
}
Any help on why I can only attack the original and not any instaniated (clone) prefabs of the same enemy.
I can have multiple prefabs hit me.. but I can't see to hit or dmg more than just the one original enemy.
Thanks alot guys.
Answer by hexagonius · Mar 09, 2017 at 06:00 PM
within your player attack trigger you're saving references to just one enemy and in on trigger enter you compare the result with it.
instead, just compare the on trigger result with the enemy tag, which will get you every enemy. if that succeeds run GetComponent on it do get the Health script and deal damage.
After reading your comment, I think this could be a possible fix? I'll have to test when I'm home ofc.
void OnTriggerEnter (Collider other)
{
if(other.gameObject == enemy)
{
enemyHealth = other.GetComponent<EnemyHealth>();
if (enemyHealth.currentHealth > 0)
{
enemyHealth.TakeDamage (attackDamage);
}
}
}
Thoughts?
Regardless thank you for your help pointing me in the right direction :)
Your answer
Follow this Question
Related Questions
Turn on Prefab with Keycode issue 2 Answers
Communicate between scenes with prefabs? 3 Answers
How to assign transforms to a Prefab?,How can I assign transforms to Prefabs? 1 Answer
Why is OnTriggerExit not firing? 3 Answers
Limit OnTriggerEnter to work with only specific Child Object Colliders 0 Answers