study/first_study/Assets/Scripts/PlayerSc/SimpleExcution.cs
jh04010421 aea5d47259 윤지호 | 유니티 연습
20226.01.22 수정 (대시구현)
2026-01-22 22:08:10 +09:00

38 lines
1.1 KiB
C#

using NUnit.Framework;
using UnityEngine;
using UnityEngine.InputSystem;
using System.Collections.Generic;
public class SimpleExcution : MonoBehaviour
{
[SerializeField] private List<GameObject> gbs = new List<GameObject>();
private GameObject target;
private void Awake()
{
gbs = new List<GameObject>(GameObject.FindGameObjectsWithTag("Enemy"));
}
void Update()
{
if (Keyboard.current.cKey.wasPressedThisFrame)
{
Vector2 pos = gameObject.transform.position;
float distance = Mathf.Infinity;
float currentdistance = 0f;
foreach(GameObject e in gbs)
{
currentdistance = Vector2.Distance(pos, e.transform.position);
if(currentdistance < distance)
{
distance = currentdistance;
target = e.gameObject;
}
}
gameObject.transform.position = target.transform.position;
gbs.Remove(target);
Destroy(target.gameObject);
}
}
}