twinStick/twinStickCrawler/Assets/Scripts/Creatures/Enemy/EnemyMove.cs

47 lines
1.3 KiB
C#
Raw Normal View History

2024-12-17 21:46:05 +00:00
using System;
using UnityEngine;
using UnityEngine.AI;
2024-12-24 20:40:18 +00:00
namespace Creatures.Enemy
2024-12-17 21:46:05 +00:00
{
2024-12-24 20:40:18 +00:00
public class EnemyMove : MonoBehaviour
{
public GameObject player;
[SerializeField]
private float playerDetectionRadius;
2024-12-17 21:46:05 +00:00
2024-12-24 20:40:18 +00:00
private NavMeshAgent _meshAgent;
2024-12-17 21:46:05 +00:00
2024-12-24 20:40:18 +00:00
private bool _playerFound = false;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
_meshAgent = GetComponent<NavMeshAgent>();
}
2024-12-17 21:46:05 +00:00
2024-12-24 20:40:18 +00:00
// Update is called once per frame
void Update()
2024-12-17 21:46:05 +00:00
{
2024-12-24 20:40:18 +00:00
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);
}
2024-12-17 21:46:05 +00:00
}
2024-12-24 20:40:18 +00:00
private void OnDrawGizmos()
2024-12-17 21:46:05 +00:00
{
2024-12-24 20:40:18 +00:00
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(transform.position, playerDetectionRadius);
2024-12-17 21:46:05 +00:00
}
}
}