20226.01.29 수정 (공격, 피격, 아이템 드랍 및 획득 구현 완료, 히트박스 트리거 버그 수정완) 다음 작업 : 공중몬스터 구현, 태그 스킬 구현
56 lines
1.5 KiB
C#
56 lines
1.5 KiB
C#
using UnityEngine;
|
|
|
|
public class ItemPickup : MonoBehaviour
|
|
{
|
|
[Header("Settings")]
|
|
[SerializeField] float speed = 2f; // 날아가는 속도
|
|
[SerializeField] float pickUpDistance = 0.5f; // 감지 범위
|
|
[SerializeField] int healAmount = 10; // 회복량
|
|
|
|
private PlayerController player;
|
|
private bool isTargeting = false; // 플레이어를 따라가는 중인지 체크
|
|
|
|
void Start()
|
|
{
|
|
// 씬에 있는 PlayerController를 찾아서 저장
|
|
player = FindAnyObjectByType<PlayerController>();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (player == null) return;
|
|
|
|
// 플레이어와의 거리 계산 (2D)
|
|
float distance = Vector2.Distance(transform.position, player.transform.position);
|
|
|
|
// 1. 감지 범위 안에 들어왔거나, 이미 따라가는 중이라면
|
|
if (distance <= pickUpDistance || isTargeting)
|
|
{
|
|
isTargeting = true; // 한 번 감지되면 끝까지 따라감 (선택 사항)
|
|
|
|
// 플레이어 쪽으로 이동
|
|
transform.position = Vector2.MoveTowards(transform.position, player.transform.position, speed * Time.deltaTime);
|
|
|
|
// 2. 플레이어 몸에 닿을 정도로 가까워지면 (획득)
|
|
if (distance < 0.05f)
|
|
{
|
|
GetItem();
|
|
}
|
|
}
|
|
}
|
|
|
|
void GetItem()
|
|
{
|
|
// 플레이어에게 체력 회복 요청
|
|
/*if (player != null)
|
|
{
|
|
player.Heal(healAmount);
|
|
}*/
|
|
|
|
// 획득 사운드 (필요시 추가)
|
|
// AudioSource.PlayClipAtPoint(clip, transform.position);
|
|
|
|
Debug.Log("아이템 획득!");
|
|
Destroy(gameObject); // 아이템 삭제
|
|
}
|
|
} |