73 lines
1.9 KiB
C#
73 lines
1.9 KiB
C#
using UnityEngine;
|
|
using Akila.FPSFramework;
|
|
using Unity.VisualScripting;
|
|
|
|
public class EnemyController : BaseAIController
|
|
{
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
protected override void Start()
|
|
{
|
|
base.Start(); // Call the base class Start method
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if (isDead)
|
|
{
|
|
HandleDeath();
|
|
return;
|
|
}
|
|
|
|
UpdateTimers();
|
|
|
|
if (!ValidatePlayerReferences())
|
|
{
|
|
StopMovement();
|
|
return;
|
|
}
|
|
|
|
float yStore = theRB.linearVelocity.y;
|
|
float distance = Vector3.Distance(player.transform.position, transform.position);
|
|
|
|
if (ShouldChasePlayer(distance))
|
|
{
|
|
HandleChaseAndAttack(distance);
|
|
}
|
|
else
|
|
{
|
|
HandlePatrolling();
|
|
}
|
|
|
|
PreserveVerticalVelocity(yStore);
|
|
}
|
|
|
|
private bool ShouldChasePlayer(float distance)
|
|
{
|
|
return distance < chaseRange && playerDamageable.health > 0;
|
|
}
|
|
|
|
private void HandleChaseAndAttack(float distance)
|
|
{
|
|
if (distance > stopCloseRange)
|
|
{
|
|
MoveTowardsTarget(player.transform, 1f); // Running speed
|
|
}
|
|
else
|
|
{
|
|
AttackTarget();
|
|
}
|
|
}
|
|
|
|
// This method will be called by Animation Events at the specific frame in the attack animation
|
|
public void DealDamage()
|
|
{
|
|
// Check if player is still in range when the damage frame occurs
|
|
if (Vector3.Distance(player.transform.position, transform.position) < 2f)
|
|
{
|
|
playerDamageable.Damage(damageAmount, playerDamageable.gameObject); // Deal damage to the player
|
|
Debug.Log("Dealt " + damageAmount + " damage to " + playerDamageable.gameObject.name); // Log the damage dealt
|
|
}
|
|
}
|
|
}
|