๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/์œ ๋‹ˆํ‹ฐ ์—”์ง„(Unity)

์œ ๋ฌผ ์ฐพ๊ธฐ ใ…Žใ…Žใ…Ž

๐ŸŽฎinspirer9 2024. 1. 30. 00:18
728x90
๋ฐ˜์‘ํ˜•

1942.unity

ํŒจ์ŠคํŠธ์บ ํผ์Šค ๋ณด๋ฉด์„œ ์ŠˆํŒ… ๊ฒŒ์ž„ ํ•œ๋ฒˆ ๋งŒ๋“ค์—ˆ๋‹ค๊ฐ€ ๋ฐฑ์—…์„ ์ž˜ ๋ชป ํ•ด์„œ์ธ์ง€ ํ”„๋ฆฌํŒน์ด๋ž‘ ์ด๋ฏธ์ง€๋งŒ ๋‚จ์•„ ์žˆ๋Š” ํ”„๋กœ์ ํŠธ, ๋ญ”๊ฐ€ ๋‹ค์‹œ ํ•ด๋ณผ๊นŒ ํ–ˆ๋Š”๋ฐ ๊ทธ๋ƒฅ ์ง€์›Œ์•ผ ๊ฒ ๋‹ค.

์ง€์šฐ๊ธฐ ์ „์— ์•„์‰ฌ์šด ๋ถ€๋ถ„๋งŒ ์ •๋ฆฌํ•ด๋‘”๋‹ค. ์ผ๋‹จ ์Šคํ”„๋ผ์ดํŠธ ๋Œ€์‹  ํ๋ธŒ๋กœ ๊ธฐ์ฒด๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค. ์‹ ๊ธฐํ•œ ๋ฐœ์ƒ์ด๋„ค? ๊ทผ๋ฐ ์ด๊ฒŒ ํญ๋ฐœํ•  ๋•Œ ๊ฐ€๋ฃจ๋กœ ๋งŒ๋“ค๋ ค๊ณ  ์ด๋ ‡๊ฒŒ ๋งŒ๋“ค์—ˆ๋˜ ๋ถ€๋ถ„์ด๋‹ค. ๋˜ ํ๋ธŒ๋ฅผ ๋ฌถ์–ด์„œ ํŒŒํŽธ์ด ๋˜๊ฒŒ ํ•  ์ˆ˜๋„ ์žˆ๊ณ ...

๊ทธ๋ž˜์„œ ์•„์‰ฌ์šด ๋งˆ์Œ์— ๋งˆ์ง€๋ง‰ ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ณด์•˜๋‹ค. ์ผ๋‹จ ์ „๋ถ€ ๊ฐ€๋ฃจ๋กœ ๋งŒ๋“ค๊ธฐ...

์ฒจ์— AddForce๋กœ ์‚ฌ๋ฐฉ์œผ๋กœ ํญ๋ฐœํ•˜๊ฒŒ ๋งŒ๋“ค์—ˆ๋”๋‹ˆ ๋‹ค๋‹ฅ๋‹ค๋‹ฅ ๋ถ™์–ด์žˆ๋Š” ๋‚ ๊ฐœ ๋ถ€๋ถ„์ด ๊ทธ๋Œ€๋กœ ์ˆ˜์ง ๋‚™ํ•˜ ํ•ด๋ฒ„๋ ธ๋‹ค.

์•ฝ๊ฐ„ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ ๋žœ๋คํ•œ ๋ฐฉํ–ฅ์œผ๋กœ ๋‚ ์•„๊ฐ€๊ฒŒ ๋งŒ๋“ค์—ˆ๋‹ค.

using UnityEngine;
using System.Collections;

public class CubeExplosion : MonoBehaviour
{
    public float explosionForce = 1000f; // ํญ๋ฐœ๋ ฅ
    public float explosionRadius = 5f; // ํญ๋ฐœ ๋ฐ˜๊ฒฝ
    public float zMoveSpeed = -2f; // Z์ถ• ์ด๋™ ์†๋„
    public Transform parentObject; // ์ƒ์œ„ ๊ฐ์ฒด

    // ์‹œ์ž‘ ์‹œ 5์ดˆ ํ›„ ํญ๋ฐœ
    void Start()
    {
        if (parentObject == null)
        {
            // ์ƒ์œ„ ๊ฐ์ฒด๊ฐ€ ์ง€์ •๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด ํ˜„์žฌ ๊ฐ์ฒด์˜ ๋ถ€๋ชจ๋ฅผ ์‚ฌ์šฉ
            parentObject = transform.parent;
        }

        StartCoroutine(ExplodeAfterDelay(3f)); // 5์ดˆ ์ง€์—ฐ
    }

    // ์ง€์—ฐ ํ›„ ํญ๋ฐœํ•˜๋Š” ์ฝ”๋ฃจํ‹ด
    IEnumerator ExplodeAfterDelay(float delay)
    {
        yield return new WaitForSeconds(delay); // ์ง€์—ฐ
        Explode(); // ํญ๋ฐœ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
    }

    // ํญ๋ฐœ ํšจ๊ณผ๋ฅผ ํŠธ๋ฆฌ๊ฑฐํ•˜๋Š” ๋ฉ”์„œ๋“œ
    public void Explode()
    {
        StartCoroutine(MoveParentOnZAxis()); // ์ƒ์œ„ ๊ฐ์ฒด Z์ถ• ์ด๋™
        // ํ”„๋ฆฌํŒน์˜ ๋ชจ๋“  ์ž์‹ ๊ฐ์ฒด(ํ๋ธŒ)์— ์ ‘๊ทผ
        foreach (Transform child in transform)
        {
            // ์ž์‹์— Rigidbody ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์—†๋‹ค๋ฉด ์ถ”๊ฐ€
            Rigidbody rb = child.gameObject.AddComponent<Rigidbody>();

            // ํญ๋ฐœ ํšจ๊ณผ ์ ์šฉ
            //rb.AddExplosionForce(explosionForce, transform.position, explosionRadius);

            // ๋žœ๋คํ•œ ๋ฐฉํ–ฅ ๋ฒกํ„ฐ ์ƒ์„ฑ
            Vector3 randomDirection = Random.insideUnitSphere * explosionForce;

            // ํญ๋ฐœ ํšจ๊ณผ ์ ์šฉ
            rb.AddForce(randomDirection);

            // ๋žœ๋คํ•œ ์‹œ๊ฐ„ ํ›„์— ๊ฐ์ฒด ํŒŒ๊ดด
            StartCoroutine(DestroyAfterRandomTime(child.gameObject));
        }
    }

    IEnumerator MoveParentOnZAxis()
    {
        while (true)
        {
            if (parentObject != null)
            {
                parentObject.Translate(0, 0, zMoveSpeed * Time.deltaTime);
            }
            yield return null;
        }
    }

    IEnumerator DestroyAfterRandomTime(GameObject obj)
    {
        float delay = Random.Range(1f, 5f); // 1~5์ดˆ ์‚ฌ์ด ๋žœ๋ค
        yield return new WaitForSeconds(delay);
        Destroy(obj);
    }
}

๊ทธ๋žฌ๋”๋‹ˆ ๊ฐ€๋ฃจ๊ฐ€ ๋˜์–ด ๋ฒ„๋ ค์„œ, z์ถ•์œผ๋กœ ๋’ค๋กœ ๋‚ ๋ผ๋ฒ„๋ ธ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•ด์•ผ ๊ฐ€๋ฃจ๊ฐ€ ๋œ ํ๋ธŒ๊ฐ€ ๋ถ€์ž์—ฐ์Šค๋Ÿฝ์ง€ ์•Š๋‹ค.

์–ด๋Š ์ •๋„๋Š” ๊ทธ๋Ÿด๋“ฏ ํ•˜์ง€๋งŒ ๋น„ํ–‰๊ธฐ๊ฐ€ ๊ฐ€๋ฃจ๊ฐ€ ๋˜๋‹ค๋‹ˆ? ์ข€ ์ด์ƒํ•˜์ง€ ์•Š์•„? ๊ฒฐ๊ตญ ๋ฉ์–ด๋ฆฌ๊ฐ€ ํ„ฐ์ง€๋Š” ๊ฒŒ ์ž์—ฐ์Šค๋Ÿฝ์ง€ ์•Š์„๊นŒ? ์ƒ๊ฐํ•˜๋ฉด์„œ ํ”„๋ฆฌํŒน์„ ์ ๋‹นํžˆ ๊ทธ๋ฃจํ•‘ ํ–ˆ๋‹ค.

๊ทธ๋Ÿด ๋“ฏ ํ•œ๋ฐ ๋„ˆ๋ฌด ๋นจ๋ฆฌ ์‚ฌ๋ผ์ง€๋‚˜? ์‹ถ์–ด์„œ z์ถ• ์†๋„๋ฅผ 1/2๋กœ ์ค„์˜€๋”๋‹ˆ ๋˜ ์–ด์ƒ‰ํ•˜๋‹ค. ํšŒ์ „์ด ์—†์–ด์„œ ๊ทธ๋Ÿฐ ๊ฒƒ ๊ฐ™๋‹ค.

ํšŒ์ „์„ ๋„ฃ์–ด๋ณด์ž! ์•„๋†”... ์ ๋‹นํ•œ ์ค‘์ ์„ ์žก์•„์ค˜์•ผ ํ•˜๋Š”๊ตฌ๋‚˜? ๊ทธ๋Ÿฌ๋„ค... ๋งž๋„ค... ๋ฌด๊ฒŒ ์ค‘์‹ฌ์„ ๊ตฌํ•ด์„œ ์ค‘์ ์„ ๊ทธ๊ณณ์œผ๋กœ ์˜ฎ๊ฒจ์•ผ ๋œ๋‹ค. ์ง€๊ธˆ ๋„ˆ๋ฌด ๋‚˜๋ญ‡์žŽ ๊ฐ™์€๋ฐ... ๋”๋Š” ๋ชป ํ•˜๊ฒ ๋‹ค. ๋‚ด์ผ์„ ์œ„ํ•ด ์ž ์„ ์ž์•ผ๊ฒ ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•