Skip to content

Instantly share code, notes, and snippets.

@clayjohn
Last active June 7, 2023 22:49
Show Gist options
  • Save clayjohn/f30aa3119ee5e59109a46f106dbef308 to your computer and use it in GitHub Desktop.
Save clayjohn/f30aa3119ee5e59109a46f106dbef308 to your computer and use it in GitHub Desktop.
For Paul
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