60 lines
1.4 KiB
C#
60 lines
1.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class EnemyMovement : MonoBehaviour
|
|
{
|
|
[Header("Elements")]
|
|
private Player player;
|
|
|
|
[Header("Settings")]
|
|
[SerializeField] private float moveSpeed;
|
|
[SerializeField] private float playerDetectionRadius;
|
|
|
|
[Header("DEBUG")]
|
|
[SerializeField] private bool gizmos;
|
|
void Start()
|
|
{
|
|
player = FindFirstObjectByType<Player>();
|
|
if(player == null)
|
|
{
|
|
Debug.LogWarning("δÕÒµ½Íæ¼Ò,×Ô¶¯Ïú»Ù");
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
FollowPlayer();
|
|
|
|
TryAttack();
|
|
}
|
|
|
|
private void FollowPlayer()
|
|
{
|
|
Vector2 direction = (player.transform.position - transform.position).normalized;
|
|
Vector2 targetPosition = (Vector2)transform.position + direction * moveSpeed * Time.deltaTime;
|
|
transform.position = targetPosition;
|
|
}
|
|
|
|
private void TryAttack()
|
|
{
|
|
float distanceToPlayer = Vector2.Distance(transform.position, player.transform.position);
|
|
if(distanceToPlayer <= playerDetectionRadius)
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
private void OnDrawGizmos()
|
|
{
|
|
if (!gizmos)
|
|
{
|
|
return;
|
|
}
|
|
Gizmos.color = Color.red;
|
|
Gizmos.DrawWireSphere(transform.position, playerDetectionRadius);
|
|
}
|
|
}
|