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("δ<>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD>,<2C>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>");
|
|||
|
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);
|
|||
|
}
|
|||
|
}
|