티스토리 뷰
# 상자에서 드랍아이템이 생성되는 로직에 문제가 있었다.
랜덤한 위치를잡고 드랍아이템에 포지션을 정해주었는데 문제는 서로 아이템들끼리 간격이 벌려지지않았다.
게임에서 드랍아이템을 획득하는 방법이 아이템과 충돌 or 아이템을 클릭 으로 획득하지만
드랍아이템들이 겹치는 문제는 꼭 해결해야했다. 아이템들끼리 겹친다면 어떤 아이템인지 확인이 불가능하다.
기존 로직에서 아이템 간격을 체크하는 부분을 추가하여 수정했다
수정한 코드이다
private HashSet<Vector2> existingPositions = new HashSet<Vector2>();
private void SetRandomPos(Transform callPosition, GameObject dropItemGo)
{
Vector2 chestPosition = callPosition.position;
float radius = 3.5f;
float minDistance = 1.5f;
int maxAttempts = 12;
int attempts = 0;
do
{
attempts++;
Vector2 randomOffset = UnityEngine.Random.insideUnitCircle.normalized * radius;
Vector2 randomPos = chestPosition + randomOffset;
bool isColliding = false;
foreach (Vector2 position in existingPositions)
{
if (Vector2.Distance(randomPos, position) < minDistance)
{
isColliding = true;
break;
}
}
if (!isColliding)
{
existingPositions.Add(randomPos);
break;
}
radius += 0.5f;
}
while (attempts < maxAttempts);
if (existingPositions.Count == 0)
{
Debug.LogWarning("위치 찾기 실패");
return;
}
int playerLayer = LayerMask.NameToLayer("Player");
int itemLayer = LayerMask.NameToLayer("item");
Physics2D.IgnoreLayerCollision(playerLayer, itemLayer);
BoxCollider2D collider = dropItemGo.GetComponent<BoxCollider2D>();
collider.enabled = false;
Vector2 randomPosition = existingPositions.Last();
dropItemGo.transform.position = callPosition.position; // 시작 위치 설정
dropItemGo.transform.DOMove(randomPosition, 1f).OnComplete(() =>
{
if (dropItemGo.CompareTag("Weapon"))
{
Vector3 originalScale = dropItemGo.transform.localScale;
float assetScalePercent = 1.5f;
Vector3 targetScale = originalScale * assetScalePercent;
dropItemGo.transform.DOScale(targetScale, 1f).SetEase(Ease.InOutQuad).SetLoops(-1, LoopType.Yoyo);
}
Physics2D.IgnoreLayerCollision(playerLayer, itemLayer, false);
collider.enabled = true;
var comp = dropItemGo.GetComponent<DropItem>();
comp.FloatingEffect(comp.transform.position);
});
}
*위치값을 List에 저장을하고 중복된 값이 있다면 다른 값을 찾게 do/while문으로 반복하며 위치를 찾았다.
아이템이 나올수 있는 최대 갯수는 12개임으로 최대 12번을 반복을 하며 위치를 찾았고
만약 12개의 위치 이상으로 나올 경우가 있으니 위치를 찾지못한다면 radius 반지름을 0.5f 로 늘려주고 다시 찾게
로직을 만들었다.
12개의 위치만 정해주니 에셋크기를 줄여아하는 고증이있었다. 그래서 12번의 시도에도 위치를 찾지못한다면
반지름을 넓히는 방법으로 생각해봤다
#Test
'게임클라이언트 프로그래밍 > Guns N Rachel's' 카테고리의 다른 글
Boss 코드 리펙토링 (0) | 2023.07.06 |
---|---|
인벤토리 용량 로직 수정 (0) | 2023.06.04 |
AES 대칭키 알고리즘을 이용한 데이터 암호화/복호화 (0) | 2023.05.29 |
UI Text변경, Dice연출 수정 (0) | 2023.05.29 |
Dice 버그 수정, 연출 추가(등급별 연출) (0) | 2023.05.25 |