Untitled
unknown
plain_text
a year ago
5.7 kB
10
Indexable
using System.Collections;
using UnityEngine;
using UnityEngine.AI;
public class EnemyAI : MonoBehaviour
{
public Transform[] waypoints; // Pole bodů, po kterých nepřítel chodí
public float detectionRadius = 10f; // Radius, ve kterém detekuje hráče
public float attackRadius = 3f; // Radius, ve kterém nepřítel začne útočit
public Transform player; // Odkaz na hráče
public float normalSpeed = 3.5f; // Normální rychlost nepřítele
public float attackSpeed = 6f; // Zvýšená rychlost při útoku
public float waitTimeAtWaypoint = 3f; // Čas, který nepřítel čeká na posledním bodě
public string debugMessage = "Hit an enemy: Scary_Teacher"; // Zpráva, na kterou nepřítel reaguje
public float goToWaypointDuration = 1f; // Doba, po kterou nepřítel jde na waypoint 0, pokud zpráva přestane přicházet
private int currentWaypointIndex = 0;
private bool isChasing = false;
private bool isAttacking = false;
private bool isGoingToWaypointZero = false;
private NavMeshAgent agent; // Komponenta pro navigaci
private float waypointTimer = 0f; // Časovač pro návrat k waypointu 0
private bool messageReceivedThisFrame = false; // Indikátor, že zpráva byla přijata
private void Start()
{
agent = GetComponent<NavMeshAgent>(); // Získání komponenty NavMeshAgent
agent.enabled = true; // Explicitní aktivace
// Inicializace rychlosti
agent.speed = normalSpeed;
// Registrace na událost Debug.Log
Application.logMessageReceived += OnLogMessageReceived;
}
private void OnDestroy()
{
// Odstranění registrace na událost při zničení objektu
Application.logMessageReceived -= OnLogMessageReceived;
}
private void Update()
{
// Pokud byla zpráva přijata v tomto snímku, resetujeme časovač návratu k waypointu 0
if (messageReceivedThisFrame)
{
isGoingToWaypointZero = true;
waypointTimer = goToWaypointDuration; // Reset časovače
}
messageReceivedThisFrame = false; // Resetujeme indikátor pro další snímek
// Pokud nepřítel jde k waypointu 0, ovládáme jeho pohyb jinak
if (isGoingToWaypointZero)
{
GoToWaypointZero();
waypointTimer -= Time.deltaTime;
// Pokud časovač vyprší, nepřítel přestane jít na waypoint 0
if (waypointTimer <= 0f)
{
isGoingToWaypointZero = false;
}
return; // Nepokračujeme s normální logikou
}
float distanceToPlayer = Vector3.Distance(transform.position, player.position);
// Rozhodování podle vzdálenosti od hráče
if (distanceToPlayer < attackRadius)
{
isAttacking = true;
isChasing = false;
}
else if (distanceToPlayer < detectionRadius)
{
isChasing = true;
isAttacking = false;
}
else
{
isChasing = false;
isAttacking = false;
}
if (isAttacking)
{
AttackPlayer();
}
else if (isChasing)
{
ChasePlayer();
}
else
{
Patrol();
}
}
private void Patrol()
{
// Pokud je agent připraven, pohni se k dalšímu bodu
if (!agent.pathPending && agent.remainingDistance < 0.5f)
{
// Pokud jsme na aktuálním waypointu, přejdeme na další
if (currentWaypointIndex < waypoints.Length)
{
agent.SetDestination(waypoints[currentWaypointIndex].position);
currentWaypointIndex++;
}
// Po dosažení posledního bodu čekáme a pak začneme znovu
if (currentWaypointIndex == waypoints.Length)
{
StartCoroutine(WaitAtLastWaypoint());
}
}
agent.speed = normalSpeed; // Reset rychlosti při patrolování
}
private void ChasePlayer()
{
// Pokud hráč je v dosahu, začni ho honit
agent.SetDestination(player.position);
agent.speed = normalSpeed; // Normální rychlost při honění hráče
}
private void AttackPlayer()
{
// Pokud hráč je v útokovém rádiusu, běž rychleji k němu
agent.SetDestination(player.position);
agent.speed = attackSpeed; // Zvýšená rychlost při útoku
}
private void GoToWaypointZero()
{
// Nastavíme cíl na waypoint s indexem 0
if (waypoints.Length > 0)
{
agent.SetDestination(waypoints[0].position);
}
agent.speed = normalSpeed; // Reset rychlosti při návratu na waypoint 0
}
private IEnumerator WaitAtLastWaypoint()
{
// Čekání na posledním bodě (můžeš změnit čas čekání)
yield return new WaitForSeconds(waitTimeAtWaypoint);
currentWaypointIndex = 0; // Po počátečním čekání začneme zase od začátku
}
private void OnLogMessageReceived(string logString, string stackTrace, LogType type)
{
// Zkontrolujeme, zda zpráva odpovídá té, kterou sledujeme
if (logString.Contains(debugMessage))
{
messageReceivedThisFrame = true; // Označíme, že zpráva přišla
}
}
}
Editor is loading...
Leave a Comment