How to get my TakeDamage script to hit/get other components?
Hi, I'm very new to C# and Unity itself, and I'm having a hard time figuring it out how to get more "targets" to get hit by my player attacks, How can my attack get the very diffents types of components in the game? Boss1, boss2, and trash mobs?
Currently I have this as my attack:
public class PlayerAttack : MonoBehaviour
{
public Animator animator;
public Transform attackPos;
public float attackRange;
public float aPS = 2f;
public float nextAttackTime;
public LayerMask whatIsEnemy;
public int damage;
void Update()
{
if (Time.time >= nextAttackTime)
{
if (Input.GetKey(KeyCode.Space))
{
Attack();
nextAttackTime = Time.time + 1f / aPS;
}
}
}
void Attack()
{
animator.SetTrigger("Player_Attack");
Collider2D[] enemiesToDamage = Physics2D.OverlapCircleAll(attackPos.position, attackRange, whatIsEnemy);
for (int i = 0; i < enemiesToDamage.Length; i++)
{
enemiesToDamage[i].GetComponent<BossHealth>().TakeDamage(damage);
}
}
void OnDrawGizmosSelected()
{
if (attackPos == null)
return;
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(attackPos.position, attackRange);
}
}
and this as my BossHealth: public class BossHealth : MonoBehaviour {
public int health = 500;
public GameObject deathEffect;
public bool isInvulnerable = false;
public void TakeDamage(int damage)
{
if (isInvulnerable)
return;
health -= damage;
if (health <= 200)
{
GetComponent<Animator>().SetBool("IsEnraged", true);
}
if (health <= 0)
{
Die();
}
}
void Die()
{
Instantiate(deathEffect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
How do I make my attack also hit0 a "Enemy" component for the neutral enemies?
Comment