2024-12-17 21:46:05 +00:00
|
|
|
using System;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.AI;
|
|
|
|
|
|
|
|
public class EnemyMove : MonoBehaviour
|
|
|
|
{
|
|
|
|
public GameObject player;
|
|
|
|
[SerializeField]
|
|
|
|
private float playerDetectionRadius;
|
|
|
|
|
|
|
|
private NavMeshAgent _meshAgent;
|
|
|
|
|
|
|
|
private bool _playerFound = false;
|
|
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
_meshAgent = GetComponent<NavMeshAgent>();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
|
|
|
Vector3 playerPos = player.transform.position;
|
|
|
|
Debug.DrawRay(transform.position, (playerPos - transform.position).normalized * playerDetectionRadius, Color.red);
|
|
|
|
|
|
|
|
if (!_playerFound && Physics.Raycast(transform.position, (playerPos - transform.position).normalized, out RaycastHit hit, playerDetectionRadius))
|
|
|
|
{
|
|
|
|
_playerFound = hit.collider.name == player.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_playerFound)
|
|
|
|
{
|
|
|
|
_meshAgent.SetDestination(playerPos);
|
|
|
|
}
|
2024-12-19 11:36:55 +00:00
|
|
|
_meshAgent.
|
2024-12-17 21:46:05 +00:00
|
|
|
}
|
|
|
|
}
|