study/first_study/Assets/Scripts/PlayerSc/SimpleExcution.cs

38 lines
1.1 KiB
C#
Raw Normal View History

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);
}
}
}