Files
CartoonFPS/Assets/Scripts/EnemyController.cs

73 lines
1.9 KiB
C#
Raw Normal View History

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