적 만들기

ㅋㅋ·2022년 6월 8일

유니티강의

목록 보기
15/24

적 상태를 여러 단계로 정의하여 로드 및 리셋 시 부하가 없도록 함

float distance = Vector3.Distance(TargetPosition, transform.position);
if (distance == 0)
{
    Arrived();
    return;
}

CurrentVelocity = (TargetPosition - transform.position).normalized * CurrentSpeed;

transform.position = Vector3.SmoothDamp(transform.position,
										TargetPosition, 
                                        ref CurrentVelocity,
                                        distance / CurrentSpeed,
                                        MaxSpeed);

등장 시에는 최대 속도로 나오며 자연스러운 감속을 위해 Vector3.SmoothDamp를 사용

CurrentSpeed = Mathf.Lerp(CurrentSpeed, 
							MaxSpeed, 
                            (Time.time - MoveStartTime) / MaxSpeedTime);

퇴장 시에는 자연스러운 가속을 위해 Mathf.Lerp를 사용

private void OnTriggerEnter(Collider other)
{
    Enemy enemy = other.GetComponentInParent<Enemy>();
    if (enemy != null)
    {
        enemy.OnCrash(this);
    }
}

public void OnCrash(Enemy enemy)
{
	...
}
    

적과 플레이어 사이에 충돌이 일어날 경우 상대방에게 이를 알려준다.

if (EnemyFileCache.ContainsKey(resourcePath))
{
    go = EnemyFileCache[resourcePath];
}
else
{
    go = Resources.Load<GameObject>(resourcePath);
    if (go == null)
    {
        Debug.LogError("load error, path: " + resourcePath);
        return null;
    }

    EnemyFileCache.Add(resourcePath, go);
}

적은 프리팹으로 만들어 필요 시 파일을 로드하고

파일 경로를 캐싱하여 로드 시 부하를 줄인다.

0개의 댓글