more abilities and fixes

This commit is contained in:
Sylvia 2026-06-12 03:54:49 -07:00
parent cb4470f2d6
commit fc2329a873
31 changed files with 268 additions and 52 deletions

View file

@ -0,0 +1,42 @@
using System;
using UnityEngine;
public class DetectEntities : MonoBehaviour
{
[SerializeField] private Entity thisEntity;
private void OnTriggerEnter2D(Collider2D other)
{
if (!other.CompareTag(thisEntity.tag) && other.TryGetComponent(out Entity isEntity))
{
thisEntity.entitiesInRange.Add(isEntity);
thisEntity.closestEntity = FindClosestEntity();
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.TryGetComponent(out Entity isEntity) && thisEntity.entitiesInRange.Contains(isEntity))
{
thisEntity.entitiesInRange.Remove(isEntity);
if (thisEntity.closestEntity == isEntity)
{
thisEntity.closestEntity = FindClosestEntity();
}
}
}
private Entity FindClosestEntity()
{
Entity currentClosestEntity = null;
foreach (Entity entityFound in thisEntity.entitiesInRange)
{
if (!currentClosestEntity ||
Vector3.Distance(entityFound.transform.position, thisEntity.transform.position) <
Vector3.Distance(currentClosestEntity.transform.position, thisEntity.transform.position))
{
currentClosestEntity = entityFound;
}
}
return currentClosestEntity;
}
}