๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ
728x90

 

์ (Enemy) ๊ฒŒ์ž„ ์˜ค๋ธŒ์ ํŠธ์— ํƒ์ง€(Detect)๋ฅผ ๋‹ด๋‹นํ•˜๋Š” ์ฝœ๋ผ์ด๋”๋ฅผ ๋‹ฌ์•„ ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ํ˜„์žฌ ์  ์˜ค๋ธŒ์ ํŠธ ํƒ์ง€ ์˜์—ญ ์•ˆ์— ์žˆ๋Š”์ง€ ์ฒดํฌํ•˜๋Š” ๋กœ์ง์„ ๊ตฌํ˜„ํ•˜๋ ค๊ณ  ํ–ˆ์—ˆ๋‹ค. ํƒ์ง€๋งŒ ํ•˜๊ณ , ์ถฉ๋Œ ์ฒ˜๋ฆฌ๋Š” ํ•˜๋ฉด ์•ˆ ๋˜๋‹ˆ ์ฝœ๋ผ์ด๋”์˜ Is Trigger ๋ชจ๋“œ๋ฅผ ํ™œ์šฉํ–ˆ๋‹ค.

 

 

๊ตฌํ˜„ํ•˜๊ณ ์ž ํ–ˆ๋˜ ๋กœ์ง ๋ฐ ์ค€๋น„ ์ž‘์—…

 

  1. ์ ์€ ํ‰์ƒ์‹œ ์ƒํƒœ(Idle)๋กœ ์—ฌ๊ธฐ ์ €๊ธฐ๋ฅผ ๋Œ์•„๋‹ค๋‹˜
  2. ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ์ ์˜ ํƒ์ง€ ์˜์—ญ์— ์ฒ˜์Œ ๋“ค์–ด์˜จ๋‹ค๋ฉด, ์ ์€ ๊ฒฝ๊ณ„ ๋ชจ๋“œ๋กœ ๋Œ์ž…
  3. ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ๊ณ„์† ์ ์˜ ํƒ์ง€ ์˜์—ญ์— 2์ดˆ ์ด์ƒ ๋จธ๋ฌด๋ฅด๊ณ  ์žˆ๋‹ค๋ฉด, ๋น ๋ฅด๊ฒŒ ๋Œ์ง„ํ•˜์—ฌ ๊ณต๊ฒฉํ•˜๋Š” ๊ณต๊ฒฉ ๋ชจ๋“œ ๋Œ์ž…
  4. ์ ์ด ๊ณต๊ฒฉ ๋ชจ๋“œ๋กœ ๋Œ์ž… ์ „์— ํƒ์ง€ ์˜์—ญ์—์„œ ๋‚˜๊ฐ„๋‹ค๋ฉด, ๋‹ค์‹œ ํ‰์ƒ์‹œ ์ƒํƒœ๋กœ ๋Œ์ž…

 

๋ฆฌ์†Œ์Šค๋Š” ์—†๊ณ , ๋กœ์ง๋งŒ ๋จผ์ € ๊ตฌํ˜„ํ•ด๋ณด๋ ค๊ณ  ํ•ด์„œ ์บก์Š์„ ์ (Enemy), ์‚ฌ๊ฐํ˜•์„ ํ”Œ๋ ˆ์ด์–ด(Player)๋กœ ์„ค์ •ํ–ˆ๋‹ค.

 

ํฐ ์ดˆ๋ก ์‚ฌ๊ฐํ˜•์ด ์ ์ด ํ”Œ๋ ˆ์ด์–ด๋ฅผ ํƒ์ง€ํ•  ํƒ์ง€ ์˜์—ญ(Detect Area)์ด๋‹ค.

 

์ (Enemy) ์˜ค๋ธŒ์ ํŠธ์˜ ํ•˜์œ„ ์˜ค๋ธŒ์ ํŠธ๋กœ ๋นˆ ๊ฒŒ์ž„ ์˜ค๋ธŒ์ ํŠธ(Empty Object)๋ฅผ ๋งŒ๋“ค๊ณ , ์—ฌ๊ธฐ์— ํƒ์ง€๋งŒ ๋‹ด๋‹นํ•˜๋Š” ์ฝœ๋ผ์ด๋”์™€ ํƒ์ง€๋ฅผ ์ œ์–ดํ•  ์Šคํฌ๋ฆฝํŠธ(script)๋ฅผ ๋‹ฌ์•„์คฌ๋‹ค.

 

์  ๊ณ„์ธต๊ตฌ์กฐ

 

์ (Enemy) ์˜ค๋ธŒ์ ํŠธ๋Š” ์—ฌ๋Ÿฌ ์˜ค๋ธŒ์ ํŠธ๋“ค๊ณผ ๋ฌผ๋ฆฌ ์ƒํ˜ธ ์ž‘์šฉ ๋ฐ ์ถฉ๋Œ์„ ํ•ด์•ผ ํ•˜๋ฏ€๋กœ, Capsule Collider 2D์™€ RigidBody 2D ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋‹ฌ์•„์คฌ๋‹ค. ์ค‘๋ ฅ์„ ์ ์šฉํ•˜๋ฉด ์  ์˜ค๋ธŒ์ ํŠธ๋Š” ์•„๋ž˜๋กœ ๋–จ์–ด์ง€๋‹ˆ, ์šฐ์„  Grivity Scale ๊ฐ’์€ 0์œผ๋กœ ์„ค์ •ํ•ด๋’€๋‹ค.

 

ํ”Œ๋ ˆ์ด์–ด(Player) ์˜ค๋ธŒ์ ํŠธ์—๋„ RigidBody 2D์™€ Box Collider 2D๋ฅผ ๋‹ฌ์•„์ฃผ๊ณ , ํƒœ๊ทธ(Tag)๋ฅผ Player๋กœ ์„ค์ •ํ–ˆ๋‹ค.

/*  RigidBody ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์žˆ์–ด์•ผ ์ฝœ๋ผ์ด๋”๋กœ๋ถ€ํ„ฐ ์ถฉ๋Œ ์ฒ˜๋ฆฌ ๋ฐ ๊ฐ์ง€์— ๋Œ€ํ•œ ๋ณด๊ณ ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.  */

 

์Šคํฌ๋ฆฝํŠธ ๋‚ด์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ…Œ์ŠคํŠธ์šฉ์œผ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋งŒ๋“ค์–ด ์คฌ๋‹ค.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Detector : MonoBehaviour
{
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag("Player"))
        {
            Debug.Log("์ ์ด ๊ฐ์ง€๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
        }
    }

    private void OnTriggerStay2D(Collider2D collision)
    {
        if (collision.CompareTag("Player"))
        {
            Debug.Log("์ ์ด ๊ณ„์† ๊ฐ์ง€๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.");
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.CompareTag("Player"))
        {
            Debug.Log("์ ์ด ์‚ฌ๋ผ์กŒ์Šต๋‹ˆ๋‹ค.");
        }
    }
}

 

 

 

์‹คํ—˜ ๋ฐ ๋ฌธ์ œ์  ํŒŒ์•…

ํ”Œ๋ ˆ์ด์–ด ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์ ์˜ ํƒ์ง€ ์˜์—ญ ์ฝœ๋ผ์ด๋”์— ๋„ฃ์–ด๋‘๊ณ , ์ž ์‹œ ๋’ค์— ๋นผ๋Š” ์‹คํ—˜์„ ํ•ด๋ดค๋‹ค.

 

์ œ๋Œ€๋กœ ๋™์ž‘ํ•œ๋‹ค.

 

์ด์ œ ์ด๊ฑธ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ตฌํ˜„ํ•˜๊ณ ์ž ํ–ˆ๋˜ ๋กœ์ง์„ ๋งŒ๋“ค์—ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์ ์ด ํƒ์ง€๋ฅผ ํ•˜๋Š” ๊ฒƒ๊นŒ์ง„ ๊ดœ์ฐฎ์€๋ฐ 2์ดˆ ์ด์ƒ ํƒ์ง€ ํ›„์— ๋Œ์ง„ํ•˜๋Š” ๋กœ์ง์ด ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜์ง€ ์•Š์•˜๋‹ค. ์งฐ๋˜ ์ฝ”๋“œ๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ๋งŒ ์†Œ๊ฐœํ•˜์ž๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Detector : MonoBehaviour
{
   float startWarningTime;
   const float DELAY = 2f;

   private void OnTriggerEnter2D(Collider collision)
   {
      if(collision.CompareTag("Player"))
      {
         startWarningTime = Time.time; 
         // ๊ฒฝ๊ณ„ ๋ชจ๋“œ ๋Œ์ž… ์ฝ”๋“œ
      }
   }

   private void OnTriggerStay2D(Collider collision)
   {
      if(collision.CompareTag("Player"))
      {
         float playerStayTime = Time.time;
         bool canRush = playerStayTime >= startWarningTime + DELAY; 
         if(canRush)
         {
            // ๊ณต๊ฒฉ ๋ชจ๋“œ๋กœ ๋Œ์ž… ์ฝ”๋“œ
         }
      }
   }
   
   private void OnTriggerExit2D(Collider collision)
   {
      if(collision.CompareTag("Player"))
      {
         // ํ‰์ƒ์‹œ ๋ชจ๋“œ๋กœ ๋Œ์ž… ์ฝ”๋“œ
      }
   }
}

 

์ฝ”๋“œ๊ฐ€ ์ด์ƒํ•œ ๊ฒƒ ๊ฐ™์ง€๋Š” ์•Š์•˜๋‹ค. Time.time ๊ฐ’์ด ์ œ๋Œ€๋กœ ๋“ค์–ด๊ฐ€์ง€ ์•Š๋Š” ๊ฒƒ์ธ๊ฐ€ ์‹ถ์–ด์„œ ๋””๋ฒ„๊ทธ๋ฅผ ํ•ด๋ดค๋Š”๋ฐ ์ œ๋Œ€๋กœ ๋“ค์–ด๊ฐ€๋Š” ๊ฒƒ์„ ํ™•์ธํ–ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ƒ๊ฐํ•ด๋ณผ ์ˆ˜ ์žˆ๋Š” ๊ฑด, OnTriggerStay() ํ•จ์ˆ˜๊ฐ€ ๊ณ„์† ํ˜ธ์ถœ๋˜๋Š”์ง€ ์—ฌ๋ถ€์˜€๋‹ค. ํ”Œ๋ ˆ์ด์–ด ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์  ํƒ์ง€ ์˜์—ญ์— ๋„ฃ์–ด๋‘๊ณ  ๊ฐ€๋งŒํžˆ ๋‘ฌ ๋ดค๋‹ค.

 

OnTriggerStay() ํ•จ์ˆ˜๊ฐ€ ์ผ์ • ํšŸ์ˆ˜ ํ˜ธ์ถœ๋˜๊ณ  ๋” ์ด์ƒ ํ˜ธ์ถœ๋˜์ง€ ์•Š๋Š”๋‹ค.

 

ํ”Œ๋ ˆ์ด์–ด ์˜ค๋ธŒ์ ํŠธ๋ฅผ ํƒ์ง€ ์˜์—ญ ๋‚ด๋ถ€์—์„œ ์‚ด์ง ์›€์ง์ธ ํ›„, ๋‹ค์‹œ ๊ฐ€๋งŒํžˆ ๋‘ฌ ๋ดค๋‹ค.

 

OnTriggerStay() ํ•จ์ˆ˜๊ฐ€ ๋‹ค์‹œ ๋ช‡ ๋ฒˆ ํ˜ธ์ถœ๋˜๋‹ค๊ฐ€ ๋” ์ด์ƒ ํ˜ธ์ถœ๋˜์ง€ ์•Š์•˜๋‹ค.

 

 

์ด๊ฑธ ๋ณด๋‹ˆ, ์™œ 2์ดˆ๊ฐ€ ์ง€๋‚˜๋”๋ผ๋„ ๋Œ์ง„์„ ์•ˆ ํ–ˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ์—ˆ๋‹ค. ์—ฌ๋Ÿฌ ๋ฒˆ ํ˜ธ์ถœ๋˜๋‹ค๊ฐ€ ์–ด๋Š ํ•œ ์‹œ์ ์—์„œ ๋Œ์ง„ ๋ชจ๋“œ๋กœ ๋“ค์–ด๊ฐ€๊ธฐ ์œ„ํ•œ ์กฐ๊ฑด์ด ์ถฉ์กฑ๋˜์—ˆ์ง€๋งŒ, ๋‹ค์Œ ํ˜ธ์ถœ์ด ์ผ์–ด๋‚˜์ง€ ์•Š์•„ ๋Œ์ง„ ๋ชจ๋“œ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ์ฝ”๋“œ๋“ค์ด ์‹คํ–‰๋˜์ง€ ์•Š์•˜๋˜ ๊ฒƒ ๊ฐ™๋‹ค.

 

๊ทธ ์ฆ๊ฑฐ๋กœ, 2์ดˆ ์ด์ƒ ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ๊ฐ์ง€ ๋˜์–ด๋„ ๋Œ์ง„์„ ์•ˆ ํ–ˆ๋˜ ์ ์ด ํ”Œ๋ ˆ์ด์–ด๋ฅผ ํƒ์ง€ ์˜์—ญ ๋‚ด๋ถ€์—์„œ ์‚ด์ง ์›€์ง์ด๋‹ˆ ๋Œ์ง„์„ ํ•˜๊ธฐ ์‹œ์ž‘ํ–ˆ๋‹ค. ํ•ด๋ณด์ง€๋Š” ์•Š์•˜์ง€๋งŒ, OnCollisionStay() ํ•จ์ˆ˜๋„ ๋น„์Šทํ•˜์ง€ ์•Š์„๊นŒ๋ž€ ์ƒ๊ฐ์ด ๋“ ๋‹ค.

 

 

ํ•ด๊ฒฐ๋ฒ•

(2023.07.10 ์ถ”๊ฐ€)

 

์นœ์ฒ ํ•˜์‹  ๋ถ„์ด ์œ„ ํ˜„์ƒ์— ๋Œ€ํ•œ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์„ ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์…จ๋‹ค.

Rigidbody ์ปดํฌ๋„ŒํŠธ๋Š” ์˜ค๋ธŒ์ ํŠธ์˜ ๋ณ€ํ™”๊ฐ€ ์ผ์ • ์‹œ๊ฐ„๋™์•ˆ ์—†์„ ๊ฒฝ์šฐ, ์Šฌ๋ฆฌํ•‘(Sleeping) ๋ชจ๋“œ๋กœ ๋Œ์ž…ํ•˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

(์ตœ์ ํ™” ๋•Œ๋ฌธ์— ๊ทธ๋Ÿฌํ•œ ๋“ฏ ํ•˜๋‹ค.)

 

์œ„ ํ˜„์ƒ๋„ ์ด๋Ÿฌํ•œ ๊ธฐ๋Šฅ ๋•Œ๋ฌธ์— ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ, ์Šฌ๋ฆฌํ•‘ ๋ชจ๋“œ๋ฅผ Never Sleep์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๋ฉด ํ•ด๊ฒฐ๋œ๋‹ค๊ณ  ํ•˜์‹ ๋‹ค.

์ข‹์€ ์ •๋ณด ์•Œ๋ ค์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :)

728x90
๋ฐ˜์‘ํ˜•