Last active
June 7, 2023 22:49
-
-
Save clayjohn/f30aa3119ee5e59109a46f106dbef308 to your computer and use it in GitHub Desktop.
For Paul
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class MoldyMeleeToast : Enemy | |
{ | |
[Header("Attack")] | |
[SerializeField] private float minimumDistanceToAttack; | |
[SerializeField] private float attackSpeed; | |
[SerializeField] private float attackDamage; | |
private Health playerHealth; | |
private AudioSource attackSound; | |
protected override void Start() | |
{ | |
base.Start(); | |
playerHealth = GetTarget().GetComponent<Health>(); | |
attackSound = GetComponent<AudioSource>(); | |
} | |
protected override void Update() | |
{ | |
base.Update(); | |
// Gets distance between itself and player | |
float distance = Vector2.Distance(rb.position, GetTarget().position); | |
// If the enemy is close enough to the player and isn't already invoking, it attacks | |
if (distance < minimumDistanceToAttack && !IsInvoking("Attack")) | |
{ | |
InvokeRepeating("Attack", attackSpeed / 2, attackSpeed); | |
} | |
else if(distance > minimumDistanceToAttack) | |
{ | |
CancelInvoke("Attack"); | |
} | |
} | |
private void Attack() | |
{ | |
playerHealth.Damage(attackDamage); | |
attackSound.Play(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment