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.