twinStick/twinStickCrawler/Assets/Scripts/Creatures/Enemy/EnemyMove.cs
2024-12-24 21:40:18 +01:00

47 lines
1.3 KiB
C#

using System;
using UnityEngine;
using UnityEngine.AI;
namespace Creatures.Enemy
{
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, LayerMask.GetMask("Player")))
{
_playerFound = true;
}
if (_playerFound)
{
_meshAgent.SetDestination(playerPos);
}
}
private void OnDrawGizmos()
{
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(transform.position, playerDetectionRadius);
}
}
}