Notice
Recent Posts
Recent Comments
Link
박민혁의 개발
특정 오브젝트 근처의 유닛과 상호 작용 하기 본문
float time = 0;
List<PlayerStatHandler> colleagueList = new List<PlayerStatHandler>();
int healP=4;
public GameObject Player;
public void Init()
{
PlayerStatHandler playerStat = transform.parent.gameObject.GetComponent<PlayerStatHandler>();
if (!colleagueList.Contains(playerStat))
{
colleagueList.Add(playerStat);
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
PlayerStatHandler target = collision.gameObject.GetComponent<PlayerStatHandler>();
if (target != null)
{
colleagueList.Add(target);
}
}
private void OnTriggerExit2D(Collider2D collision)
{
PlayerStatHandler target = collision.gameObject.GetComponent<PlayerStatHandler>();
if (target != null)
{
colleagueList.Remove(target);
}
}
private void FixedUpdate()
{
time += Time.deltaTime;
if (time >= 1f && (colleagueList.Count>=1))
{
if (colleagueList.Count >= 1)
{
Heal();
time = 0F;
}
}
}
private void Heal()
{
for (int i = 0; i < colleagueList.Count; ++i)
{
if (!colleagueList[i].isDie)
{
colleagueList[i].HPadd(healP);
}
}
}
트리거 스테이 사용시 매프레임 호출 되기에
엔터 엑시트를 이용한 리스트 관리로 최적화
오브젝트 근처에 있을것 이 조건이니 트리거스테이를 생각했는데
아마 특정 오브젝트 근처에 가면 상호 작용 되는 것들이 이런식으로 되는게 아닐까 추측한다
ex 테라리아 작업대 , 이터널리턴 모닥불