์๋ ๊ธ์ ์ฐธ๊ณ ํ์ต๋๋ค.
[Unity] ํน์ ๋ฒ์ ๋ด์์ ๋๋คํ ์์น์ ์ค๋ธ์ ํธ ์คํฐํ๊ธฐ
unity์์ ํน์ ๋ฒ์ ๋ด์ ๋๋คํ ์์น์ ์ค๋ธ์ ํธ๋ฅผ ์์ฑํ๋ ๋ฐฉ๋ฒ์ ์์๋ณด๊ฒ ์ต๋๋ค. ์ฐ์ unity์์ ์ํ๋ ๋ฒ์๋ฅผ ํน์ ํด๋ด ์๋ค. .png) ์ ๋ ์ด Plane ์ค๋ธ์ ํธ ์์์ ๋๋คํ ์์น์ ์บก์์ด ์์ฑ
velog.io
๐ ๋ฒ์ ์ง์
์ํ๋ ๋ฒ์๋ฅผ ์ฝ๋ผ์ด๋๋ก ์ ํฉ๋๋ค.
๐ ๋๋คํ ์์น ์ป๋ ํจ์
์ฝ๋ผ์ด๋ ์ฌ์ด์ฆ๋ฅผ ์ด์ฉํ์ฌ ์ฝ๋ผ์ด๋ ๋ด์ ๋๋คํ ๊ฐ์ ์ป์ ์์ ์ ๋๋ค.
// ๋๋ค ์์น ์ป๊ธฐ
Vector3 GetRandomPosition() {
GameObject balloonRange = transform.Find("Balloons").gameObject; // Balloons ์ค๋ธ์ ํธ
BoxCollider2D rangeCollider = balloonRange.GetComponent<BoxCollider2D>();
Vector3 originPosition = balloonRange.transform.position;
// ์ฝ๋ผ์ด๋ ํ์ ์์ฑ ๋ฒ์
// ์ฝ๋ผ์ด๋์ size
float rangeX = rangeCollider.bounds.size.x;
float rangeY = rangeCollider.bounds.size.y;
// ์ฝ๋ผ์ด๋์ offset
Vector3 offset = rangeCollider.offset;
rangeX = Random.Range((rangeX / 2) * -1, rangeX / 2);
rangeY = Random.Range((rangeY / 2) * -1, rangeY / 2);
Vector3 RandomPostion = new Vector3(rangeX, rangeY, 0f) + offset;
Vector3 respawnPosition = originPosition + RandomPostion;
return respawnPosition;
}
๐ ์ค๋ธ์ ํธ ์์ฑ
์์์ ์์ฑํ ํจ์๋ฅผ ์ฌ์ฉํด ๊ฒ์ ์ค๋ธ์ ํธ๋ฅผ ์์ฑํ๊ฒ ์ต๋๋ค.
์์ฑํ ์ค๋ธ์ ํธ๋ "Balloons" ์ค๋ธ์ ํธ๋ฅผ ๋ถ๋ชจ๋ก์จ, ์์ ์ค๋ธ์ ํธ๋ก ์ง์ ํ๊ฒ ์ต๋๋ค.
๋ํ, ์์ฑํ ๊ฐ์๋งํผ ์์ฑํ๊ฒ ์ต๋๋ค.
public int balloonCnt; // ์์ฑํ ํ์ ๊ฐ์
public GameObject balloonPrefab; // ํ์ ํ๋ฆฌํน
private void Update() {
// ํ์ฌ ํ์ ๊ฐ์ ํ์ธ
int CurrentBalloonCnt = transform.Find("Balloons").childCount;
if (CurrentBalloonCnt >= balloonCnt) {
return;
}
// GetRandomPosition() ํจ์๋ฅผ ์ด์ฉํ์ฌ ๋๋คํ ์์น์ ํ์ ์์ฑ
GameObject balloonClone = Instantiate(balloonPrefab, GetRandomPosition(), Quaternion.identity);
// ๋ถ๋ชจ์ ์์์ผ๋ก ์ ๋ฆฌ
balloonClone.transform.SetParent(transform.Find("Balloons"));
}
๐ ์ค๋ธ์ ํธ ํ๊ดด
์์ฑ์ ํ์ผ๋ฉด ํ๊ดด๋ ํด์ผ๊ฒ ์ฃต
public int balloonCnt; // ์์ฑํ ํ์ ๊ฐ์
public GameObject balloonPrefab; // ํ์ ํ๋ฆฌํน
private void Update() {
// ํ์ฌ ํ์ ๊ฐ์ ํ์ธ
int CurrentBalloonCnt = transform.Find("Balloons").childCount;
// ๋ง์ผ๋ฉด ํ๊ดด
if (CurrentBalloonCnt >= balloonCnt) {
Transform[] childList = transform.Find("Balloons").GetComponentsInChildren<Transform>();
for (int i = CurrentBalloonCnt; i > balloonCnt; i--) {
Destroy(childList[i].gameObject);
}
return;
}
// GetRandomPosition() ํจ์๋ฅผ ์ด์ฉํ์ฌ ๋๋คํ ์์น์ ํ์ ์์ฑ
GameObject balloonClone = Instantiate(balloonPrefab, GetRandomPosition(), Quaternion.identity);
// ๋ถ๋ชจ์ ์์์ผ๋ก ์ ๋ฆฌ
balloonClone.transform.SetParent(transform.Find("Balloons"));
}
์ ์ฒด ์ฝ๋
public int balloonCnt; // ์์ฑํ ํ์ ๊ฐ์
public GameObject balloonPrefab; // ํ์ ํ๋ฆฌํน
private void Update() {
// ํ์ฌ ํ์ ๊ฐ์ ํ์ธ
int CurrentBalloonCnt = transform.Find("Balloons").childCount;
// ๋ง์ผ๋ฉด ํ๊ดด
if (CurrentBalloonCnt >= balloonCnt) {
Transform[] childList = transform.Find("Balloons").GetComponentsInChildren<Transform>();
for (int i = CurrentBalloonCnt; i > balloonCnt; i--) {
Destroy(childList[i].gameObject);
}
return;
}
// GetRandomPosition() ํจ์๋ฅผ ์ด์ฉํ์ฌ ๋๋คํ ์์น์ ํ์ ์์ฑ
GameObject balloonClone = Instantiate(balloonPrefab, GetRandomPosition(), Quaternion.identity);
// ๋ถ๋ชจ์ ์์์ผ๋ก ์ ๋ฆฌ
balloonClone.transform.SetParent(transform.Find("Balloons"));
}
// ๋๋ค ์์น ์ป๊ธฐ
Vector3 GetRandomPosition() {
GameObject balloonRange = transform.Find("Balloons").gameObject; // Balloons ์ค๋ธ์ ํธ
BoxCollider2D rangeCollider = balloonRange.GetComponent<BoxCollider2D>();
Vector3 originPosition = balloonRange.transform.position;
// ์ฝ๋ผ์ด๋ ํ์ ์์ฑ ๋ฒ์
// ์ฝ๋ผ์ด๋์ size
float rangeX = rangeCollider.bounds.size.x;
float rangeY = rangeCollider.bounds.size.y;
// ์ฝ๋ผ์ด๋์ offset
Vector3 offset = rangeCollider.offset;
rangeX = Random.Range((rangeX / 2) * -1, rangeX / 2);
rangeY = Random.Range((rangeY / 2) * -1, rangeY / 2);
Vector3 RandomPostion = new Vector3(rangeX, rangeY, 0f) + offset;
Vector3 respawnPosition = originPosition + RandomPostion;
return respawnPosition;
}