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

[์œ ๋‹ˆํ‹ฐ] ํŠน์ • ๊ฒŒ์ž„ ์˜ค๋ธŒ์ ํŠธ ๊ฐ์ง€ ๊ตฌํ˜„ (ex.์•„์ดํ…œ ๋จน๊ธฐ)

Jaeseo Kim 2023. 1. 18. 23:58

์ถฉ๋Œ ๊ฐ์ง€

์œ ๋‹ˆํ‹ฐ์˜ ๋‚ด์žฅ๋œ ๋ฌผ๋ฆฌ ์‹œ์Šคํ…œ์„ ์‚ฌ์šฉํ•˜์—ฌ ์บ๋ฆญํ„ฐ์˜ GameObject๊ฐ€ ์•„์ดํ…œ์˜ GameObject์™€ ์ถฉ๋Œํ•  ๋•Œ๋ฅผ ๊ฐ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์บ๋ฆญํ„ฐ ์Šคํฌ๋ฆฝํŠธ์˜ "OnCollisionEnter" ๋˜๋Š” "OnTriggerEnter" ๋ฉ”์„œ๋“œ์—์„œ ์ถฉ๋Œํ•˜๋Š” ๊ฒƒ์„ ๊ฐ์ง€ํ•ฉ๋‹ˆ๋‹ค.

 

01. Tag

์œ ๋‹ˆํ‹ฐ ์—๋””ํ„ฐ์ฐฝ์—์„œ GameObjects ํ•ญ๋ชฉ์— ํƒœ๊ทธ๋ฅผ ํ• ๋‹นํ•œ ๋‹ค์Œ
์บ๋ฆญํ„ฐ ์Šคํฌ๋ฆฝํŠธ์˜ "OnCollisionEnter" ๋˜๋Š” "OnTriggerEnter" ๋ฉ”์„œ๋“œ์—์„œ ์ถฉ๋Œํ•˜๋Š” ๊ฐœ์ฒด์˜ ํƒœ๊ทธ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

void OnCollisionEnter(Collision collision)
{
    if (collision.gameObject.CompareTag("Item"))
    {
        // The colliding object is an item
    }
}

 

02. Layer

GameObjects ํ•ญ๋ชฉ์— ๋ ˆ์ด์–ด๋ฅผ ํ• ๋‹นํ•œ ๋‹ค์Œ ์บ๋ฆญํ„ฐ ์Šคํฌ๋ฆฝํŠธ์˜ "OnCollisionEnter" ๋˜๋Š” "OnTriggerEnter" ๋ฉ”์„œ๋“œ์—์„œ ์ถฉ๋Œํ•˜๋Š” ๊ฐ์ฒด์˜ ๋ ˆ์ด์–ด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

void OnCollisionEnter(Collision collision)
{
    if (collision.gameObject.layer == LayerMask.NameToLayer("Item"))
    {
        // The colliding object is an item
    }
}

 

03. Script Component

์œ ๋‹ˆํ‹ฐ ์—๋””ํ„ฐ์ฐฝ์—์„œ ํ•ญ๋ชฉ ์†์„ฑ์„ ์ •์˜ํ•˜๋Š” ์Šคํฌ๋ฆฝํŠธ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ GameObjects ํ•ญ๋ชฉ์— ํ• ๋‹นํ•œ ๋‹ค์Œ

์ถฉ๋Œํ•˜๋Š” ๊ฐœ์ฒด์— ์บ๋ฆญํ„ฐ ์Šคํฌ๋ฆฝํŠธ์˜ "OnCollisionEnter" ๋˜๋Š” "OnTriggerEnter" ๋ฉ”์„œ๋“œ์—์„œ ํ•ด๋‹น ์Šคํฌ๋ฆฝํŠธ ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

void OnCollisionEnter(Collision collision)
{
    Item itemScript = collision.gameObject.GetComponent<Item>();
    if (itemScript != null)
    {
        // The colliding object is an item
    }
}