๐Ÿ’ป My Work/๐ŸŽฎ Unity

[์œ ๋‹ˆํ‹ฐ] 2D ํŠน์ • ๋ฒ”์œ„ ๋‚ด์˜ ๋žœ๋คํ•œ ์œ„์น˜์— ๊ฒŒ์ž„ ์˜ค๋ธŒ์ ํŠธ ์ƒ์„ฑํ•˜๊ธฐ

Jaeseo Kim 2023. 1. 21. 00:28

ํ’์„  ์ƒ์„ฑ...

 

์•„๋ž˜ ๊ธ€์„ ์ฐธ๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

[Unity] ํŠน์ • ๋ฒ”์œ„ ๋‚ด์—์„œ ๋žœ๋คํ•œ ์œ„์น˜์— ์˜ค๋ธŒ์ ํŠธ ์Šคํฐํ•˜๊ธฐ

unity์—์„œ ํŠน์ • ๋ฒ”์œ„ ๋‚ด์— ๋žœ๋คํ•œ ์œ„์น˜์— ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  unity์—์„œ ์›ํ•˜๋Š” ๋ฒ”์œ„๋ฅผ ํŠน์ •ํ•ด๋ด…์‹œ๋‹ค. .png) ์ €๋Š” ์ด Plane ์˜ค๋ธŒ์ ํŠธ ์œ„์—์„œ ๋žœ๋คํ•œ ์œ„์น˜์— ์บก์Š์ด ์ƒ์„ฑ

velog.io

 

๐Ÿ“Œ ๋ฒ”์œ„ ์ง€์ •

์›ํ•˜๋Š” ๋ฒ”์œ„๋ฅผ ์ฝœ๋ผ์ด๋”๋กœ ์ •ํ•ฉ๋‹ˆ๋‹ค.

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;
    }

 

๐Ÿ“Œ ์˜ค๋ธŒ์ ํŠธ ์ƒ์„ฑ

์œ„์—์„œ ์ž‘์„ฑํ•œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๊ฒŒ์ž„ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์ƒ์„ฑํ•œ ์˜ค๋ธŒ์ ํŠธ๋Š” "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;
}