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

 

 

 

์ด ๋•Œ๊นŒ์ง€ ํ”Œ๋ ˆ์ด์–ด ์บ๋ฆญํ„ฐ, ์กฐ๋ช… ์ฒ˜๋ฆฌ, ๋ฐฐ๊ฒฝ ์„ธํŒ… ์ž‘์—…์„ ์ง„ํ–‰ํ–ˆ์—ˆ๋‹ค. ์˜ค๋Š˜์€ ํ”Œ๋ ˆ์ด์–ด๋ฅผ ๋ฐฉํ•ดํ•  ๊ฒฝ๊ด€(Police) ํŒจํ„ด์„ ๋งŒ๋“ค์–ด๋ณผ ์ƒ๊ฐ์ด๋‹ค.

 

์˜ค๋Š˜์˜ ์ฃผ์ธ๊ณต, ๊ฒฝ๊ด€

 

๊ณตํฌ ๊ฒŒ์ž„ ํŠน์„ฑ์ƒ, ์ข€ ์–ด๋‘์›Œ ๋ณด์—ฌ์•ผ ํ•˜๊ธฐ์— ์œ ๋‹ˆํ‹ฐ ์Šคํ”„๋ผ์ดํŠธ ๋ Œ๋”๋Ÿฌ์—์„œ ์ƒ‰๊ฐ์„ ์–ด๋‘ก๊ฒŒ ๋‚ฎ์ท„๋‹ค. ์ด์ œ ์ฐจ๋ก€๋Œ€๋กœ ํ•œ ๋ฒˆ ๋งŒ๋“ค์–ด๋ณด์ž.

 

 

 

1. ๊ฒฝ๊ด€ ์™ธ๊ด€ ์„ธํŒ…ํ•˜๊ธฐ

 

ํ‰์†Œ ์ƒํƒœ(Idle), ๊ฑท๊ธฐ(Walk), ์ธ๊ธฐ์ฒ™ ๊ฐ์ง€(Feel) ์• ๋‹ˆ๋ฉ”์ด์…˜์€ ๋‹ค๋ฅธ ํŒ€์›์ด ๋งŒ๋“ค์–ด์ค€ ์ƒํƒœ๋‹ค. ๋”ฐ๋ผ์„œ, ์ด๋ฒˆ์—๋Š” ๋”ฐ๋กœ ์• ๋‹ˆ๋ฉ”์ด์…˜ ์ž‘์—…์„ ์•ˆ ํ•ด๋„ ๋๋‹ค. ๋‹ค๋งŒ, ์›๋ž˜ ๊ธฐํš์„œ ์ƒ์—๋Š” ์ด๋Ÿฐ ๋‚ด์šฉ์ด ์—†์—ˆ์ง€๋งŒ ์šฐ๋ฆฌ๊ฐ€ ๊ฐœ๋ฐœํ•˜๋Š” ๊ฒŒ์ž„์ด ๊ณตํฌ๊ฒŒ์ž„์ธํผ ์ข€ ๋” ํ™•์‹คํ•œ ์ดํŽ™ํŠธ๋ฅผ ์ฃผ๊ณ  ์‹ถ์—ˆ๋‹ค.

 

์›๋ž˜ ์ธ๊ฒŒ์ž„ ๋ฐ๊ธฐ๋กœ ์บก์ณํ•˜๋‹ˆ, ๋„ˆ๋ฌด ์•ˆ ๋ณด์—ฌ์„œ ๋ฐ๊ธฐ๋ฅผ ์‚ด์ง ์˜ฌ๋ ธ๋‹ค.

 

๊ฒฝ๊ด€์ด ๋“ค๊ณ  ์žˆ๋Š” ์†์ „๋“ฑ์— ๋ผ์ดํŠธ(Light)๋ฅผ ๋‹ฌ์•„์คฌ๋‹ค. ํฐ์ƒ‰์ด๋‚˜ ๋…ธ๋ž€์ƒ‰ ์กฐ๋ช…์€ ์˜คํžˆ๋ ค ํ”Œ๋ ˆ์ด์–ด๋ฅผ ๊ตฌํ•˜๋Ÿฌ ์˜จ ๊ฒฝ๊ด€ ๋Š๋‚Œ์ด ๋‚ฌ๊ธฐ ๋•Œ๋ฌธ์— ์ฒ˜์Œ ๋”ฑ ๋ดค์„ ๋•Œ, "์•„, ์ ์ด๊ตฌ๋‚˜!"๋ผ๋Š” ๊ฑธ ๋Š๋‚„ ์ˆ˜ ์žˆ๋„๋ก ๋นจ๊ฐ„์ƒ‰ ์กฐ๋ช…์œผ๋กœ ํ•ด์คฌ๋‹ค. ํ‰์ƒ ์‹œ์— ์ˆœ์ฐฐํ•˜๋ฉฐ ๊ฑธ์„ ๋•Œ๋Š” ์ €๋Ÿฐ ๋ชจ์Šต์œผ๋กœ ๊ฑธ์–ด๋‹ค๋‹ˆ๋Š” ๊ฒƒ์ด๋‹ค.

 

๊ทธ๋Ÿฐ๋ฐ, ๋˜ ํ•˜๋‚˜ ๊ณ ๋ฏผ์ด ์ƒ๊ฒผ์—ˆ๋‹ค. ์ด๊ฒƒ ๋˜ํ•œ, ๊ธฐํš์„œ ์ƒ์—๋Š” ์—†๋Š” ๋‚ด์šฉ์ด์—ˆ๋‹ค. ๋งŒ์•ฝ ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ์ ์—๊ฒŒ ๋“ค์ผฐ์„ ๋•Œ, ๊ฒฝ๊ด€์ด ์ถ”์  ์ƒํƒœ์— ์žˆ๋‹ค๋Š” ๊ฑธ ํ”Œ๋ ˆ์ด์–ด์—๊ฒŒ ์–ด๋–ป๊ฒŒ ๊ฐ์ธ์‹œ์ผœ์ค„๊นŒ?

 

ํ”Œ๋ ˆ์ด์–ด๋ฅผ ๋ฐœ๊ฒฌํ•˜์—ฌ ์ถ”์  ์ƒํƒœ์— ๋“ค์–ด๊ฐ„ ๊ฒฝ๊ด€

 

์œ„์™€ ๊ฐ™์ด ํ‘œํ˜„ํ•˜๋ฉด ๋˜๊ฒŒ ์‹ค๊ฐ๋‚˜๊ณ  ์ข‹์ง€ ์•Š์„๊นŒ๋ž€ ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค. ์ˆ˜์ง€์˜ ์ง‘ ์ด๋ฒคํŠธ๋‚˜ ๋ฌด๋‹น์ง‘ ์•ž ๋Œ€๋ฌธ์—์„œ ์‚ฌ์šฉํ–ˆ๋˜ Freeform 2D Light๋ฅผ ๊ฒฝ๊ด€์—๊ฒŒ๋„ ๋ฐฐ์น˜ํ•ด์คฌ๋‹ค. ํ‰์ƒ ์‹œ์—๋Š” ์ด ๋ถˆ๋น›์ด ๋น„ํ™œ์„ฑํ™” ๋˜์–ด ์žˆ๋‹ค๊ฐ€, ๊ฒฝ๊ด€์ด ์ถ”์  ์ƒํƒœ์— ๋“ค์–ด๊ฐ€๋ฉด ์œ„์™€ ๊ฐ™์ด ํ™œ์„ฑํ™”๊ฐ€ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

 

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด, ์™ธ๊ด€ ์„ธํŒ…์€ ๋Œ€๊ฐ• ๋‹ค ํ•œ ๊ฒƒ ๊ฐ™๋‹ค.

 

 

 

2. ํƒ์ง€ ๋ฐ ์ถ”๊ฒฉ ํŒ์ • ์˜์—ญ ์„ค์ •ํ•˜๊ธฐ

 

๊ฒฝ๊ด€ ๋˜ํ•œ ๋ฌผ๋ฆฌํšจ๊ณผ๋ฅผ ๋ฐ›์•„์•ผ ํ•˜๊ธฐ์— Rigidbody 2D ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋‹ฌ์•„์คฌ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ณธ๋ž˜ ๋ชธ์ฒด, ๊ฒฝ๊ด€์˜ ๋’ท ๋ถ€๋ถ„์— ํƒ์ง€์šฉ, ๊ฒฝ๊ด€์˜ ์•ž ๋ถ€๋ถ„ ํ”Œ๋ ˆ์ด์–ด ๋ฐœ๊ฒฌ, ๊ฒŒ์ž„์˜ค๋ฒ„ ์˜์—ญ์— ์‚ฌ์šฉํ•  ์ฝœ๋ผ์ด๋” ์ด๋ ‡๊ฒŒ 4๊ฐœ๊ฐ€ ํ•„์š”ํ–ˆ๋‹ค.

 

๊ฒฝ๊ด€์˜ ์ฝœ๋ผ์ด๋” ์„ธํŒ… ๋ชจ์Šต

 

๋ฌผ๋ก , ๋ณธ์ฒด ์ฝœ๋ผ์ด๋”๋ฅผ ์ œ์™ธํ•˜๊ณ ๋Š” ๊ฐ ์ฝœ๋ผ์ด๋”๋งˆ๋‹ค ํŠธ๋ฆฌ๊ฑฐ ์ด๋ฒคํŠธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ ์˜ˆ์ •์ด๊ธฐ์— ๋นˆ ์˜ค๋ธŒ์ ํŠธ๋“ค์„ ๋งŒ๋“ค์–ด์„œ ๊ฐ๊ฐ์— ๋ฐฐ์น˜ํ•ด์คฌ๋‹ค. ๋ณธ์ฒด ์ฝœ๋ผ์ด๋” ๋นผ๊ณ  ์ „๋ถ€ "is Trigger"๋ฅผ ํ™œ์„ฑํ™”ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. ์•„์ง ๊ฒŒ์ž„ ์˜ค๋ฒ„ ํ™”๋ฉด ๋ฆฌ์†Œ์Šค๊ฐ€ ์™„์„ฑ๋˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๊ธฐ์„œ ๊ฒŒ์ž„ ์˜ค๋ฒ„๊นŒ์ง€๋Š” ๋‹ค๋ฃจ์ง€ ์•Š์„ ์˜ˆ์ •์ด๋‹ค.

 

์ด์ œ ์Šคํฌ๋ฆฝํŠธ ์ž‘์„ฑํ•˜๋Š” ์ผ๋งŒ ๋‚จ์•˜๋‹ค.

 

 

 

3. ๊ฒฝ๊ด€ ํŒจํ„ด ๋งŒ๋“ค๊ธฐ

 

๋จผ์ €, ์–ด๋–ค ํŒจํ„ด๋“ค์„ ๋งŒ๋“ค์–ด์•ผ ํ•˜๋Š”์ง€ ํ•œ ๋ฒˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‚˜์—ดํ•ด๋ดค๋‹ค.

  • ํ‰์†Œ ์ƒํƒœ์ผ ๋•, ๋žœ๋คํ•˜๊ฒŒ ์ด๋ฆฌ ์ €๋ฆฌ ๋‹ค๋‹ˆ๋ฉฐ ์ˆœ์ฐฐ
  • ๊ฒฝ๊ด€ ๋’ค์— ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ์ผ์ • ๊ฑฐ๋ฆฌ ๋‚ด์— ๋“ค์–ด์˜จ๋‹ค๋ฉด, ์ธ๊ธฐ์ฒ™์„ ๋Š๋ผ๊ณ  ์ž ๊น ๋จธ๋ญ‡๊ฑฐ๋ฆฌ๋‹ค๊ฐ€ ๋’ค๋Œ์•„๋ด„
    • ๋’ค๋Œ์•„๋ดค๋Š”๋ฐ ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ์—†๋‹ค๋ฉด ๋‹ค์‹œ ํ‰์†Œ ์ƒํƒœ, ์žˆ๋‹ค๋ฉด ์ถ”๊ฒฉ ์‹œ์ž‘
  • ๊ฒฝ๊ด€ ์•ž์— ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ์ผ์ • ๊ฑฐ๋ฆฌ ๋‚ด์— ๋“ค์–ด์˜จ๋‹ค๋ฉด, ์ถ”์  ์ƒํƒœ๋กœ ๋ณ€ํ•˜์—ฌ ํ”Œ๋ ˆ์ด์–ด ์ถ”๊ฒฉ ์‹œ์ž‘
  • ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ์ถ”๊ฒฉ ๋‹นํ•˜๋Š” ๋„์ค‘์— ์˜ค๋ธŒ์ ํŠธ ๋’ค์— ์ˆจ์—ˆ๋‹ค๋ฉด, ๊ฒฝ๊ด€์€ ํ”Œ๋ ˆ์ด์–ด๋ฅผ ์ฐพ์•„ ์ด๋ฆฌ์ €๋ฆฌ ๋‹ค๋‹˜
  • ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ์ˆจ๊ณ  ๋‚˜์„œ, ์ผ์ • ์‹œ๊ฐ„์ด ์ง€๋‚˜๋ฉด ๊ฒฝ๊ด€์€ ๋‹ค์‹œ ํ‰์†Œ ์ƒํƒœ๋กœ ๋Œ์•„์™€์„œ ์ˆœ์ฐฐ ์‹œ์ž‘
  • "ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ๊ฒฝ๊ด€์—๊ฒŒ ์žกํžˆ๋ฉด ๊ฒŒ์ž„ ์˜ค๋ฒ„" ํŒจํ„ด์€ ๋‚˜์ค‘์— ๋ฆฌ์†Œ์Šค๊ฐ€ ๋งŒ๋“ค์–ด์ง€๋ฉด ๊ทธ ๋•Œ ๊ตฌํ˜„

 

 

์ด๋ ‡๊ฒŒ ๋‚˜์—ด๋งŒ ํ•ด๋ดค๋Š”๋ฐ, ๋ฒŒ์จ ๋จธ๋ฆฌ๊ฐ€ ์•„ํŒ ๋‹ค. ์–ด๋–ป๊ฒŒ ๋จผ์ € ๊ตฌ์กฐ ํ‹€์„ ์žก๊ณ  ์‹œ์ž‘ํ•ด์•ผํ• ๊นŒ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ ํŒ€์›์ด ๋””์ž์ธ ํŒจํ„ด ์ค‘์— ์ƒํƒœ ํŒจํ„ด์ด๋ž€ ๊ฑธ ์ฐพ์•˜๋‹ค๊ณ  ๋งํ•ด์คฌ๋‹ค. ๊ทธ๋ž˜์„œ, ๊ทธ๊ฑธ ๋จผ์ € ๊ณต๋ถ€ํ•˜๊ณ  ๋”ฐ๋ผํ•ด๋ณด๋ฉฐ ๋งŒ๋“ค๊ธด ํ–ˆ๋Š”๋ฐ ์ด๋ ‡๊ฒŒ ํ•˜๋Š” ๊ฒŒ ๋งž๋Š”์ง€๋Š” ์†”์งํžˆ ์˜๋ฌธ์ด๋‹ค...ใ…Žใ…Žใ…Ž

/*  < ์ฐธ๊ณ ํ•œ ๊ณต๋ถ€ ๋ธ”๋กœ๊ทธ> (๊ณต๋ถ€ํ•˜๋Š” ์‹๋นต๋ง˜ ๋‹˜), (๊ธ€๋ฆญ์˜ ๋งŒ๋“ค์–ด๊ฐ€๋Š” ์„ธ์ƒ ๋‹˜)(๊น€์„ ๋ฏผ ๋ฒจ๋กœ๊ทธ ๋‹˜)  */

 

 

StateMachine.cs

 

์ƒํƒœ๋“ค๋„ ์ƒ๋ช… ์ฃผ๊ธฐ๊ฐ€ ์žˆ๋‹ค๋Š” ๊ธ€์„ ๋ณด๊ณ  ๋จผ์ € ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค. Enemy๋Š” ๊ฒฝ๊ด€์— ๋ถ€์ฐฉํ•  ํด๋ž˜์Šค๋‹ค.

public interface IState
{
    void StateEnter(Enemy enemy);
    void StateFixedUpdate(Enemy enemy);
    void StateUpdate(Enemy enemy);
    void StateExit(Enemy enemy);
}

 

์ƒํƒœ์— ๋“ค์–ด๊ฐ”์„ ๋•Œ(StateEnter), ๋ฌผ๋ฆฌ ์—ฐ์‚ฐ๊ณผ ๊ด€๋ จ๋œ ๋กœ์ง์„ ์‚ฌ์šฉํ•˜๋Š” ์ƒํƒœ(StateFixedUpdate), ๋งค ํ”„๋ ˆ์ž„๋งˆ๋‹ค ์‹คํ–‰๋˜๋Š” ์ƒํƒœ(StateUpdate), ์ƒํƒœ๋ฅผ ๋ฒ—์–ด๋‚  ๋•Œ ์‹คํ–‰(StateExit) ์ด๋ ‡๊ฒŒ ๊ตฌ์„ฑํ–ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋Ÿฐ ์ƒํƒœ๋“ค์„ ์ œ์–ดํ•ด์ค„ StateMachine ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค.

public class StateMachine
{
    public IState currentState { get; private set; }


    public StateMachine(Enemy enemy, IState defaultState)
    {
        currentState = defaultState;
        currentState.StateEnter(enemy);
    }
    
    public void SetState(Enemy enemy, IState state)
    {

        if (currentState == null || currentState == state)
        {
            Debug.Log("์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
            return;
        }

        currentState.StateExit(enemy);
        currentState = state;
        currentState.StateEnter(enemy);
    }

    public void UpdateState(Enemy enemy)
    {
        currentState.StateUpdate(enemy);
    }

    public void FixedUpdateState(Enemy enemy)
    {
        currentState.StateFixedUpdate(enemy);
    }
}

 

์ด๊ฑธ ๋ฐ”ํƒ•์œผ๋กœ ์ƒํƒœ ํด๋ž˜์Šค๋“ค์„ ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์œผ๋กœ ์ •์˜ํ•˜์˜€๋‹ค. (์ƒํƒœ ์ „ํ™˜๋งˆ๋‹ค ์ƒˆ๋กœ์šด ๊ฐ์ฒด ์ƒ์„ฑ์€ ๊ฐ€๋น„์ง€๋ผ๊ณ  ํŒ๋‹จ)

public class Idle : IState          // ํ‰์†Œ ์ƒํƒœ
{
    private static Idle Instance = new Idle();
    private Idle() { }
    public static Idle GetInstance() {  return Instance; }
     ...
}

public class FeelStrange : IState   // ์ธ๊ธฐ์ฒ™์„ ๋Š๋ผ๋Š” ์ƒํƒœ
{
    private static FeelStrange Instance = new FeelStrange();
    private FeelStrange() { }
    public static FeelStrange GetInstance()
    {
        return Instance;
    }
    
     ...
}

public class ChaseState : IState    // ์ถ”๊ฒฉ ์ƒํƒœ
{
    private static ChaseState Instance = new ChaseState();
    private ChaseState() { }
    public static ChaseState GetInstance()
    {
        return Instance;
    }
    
     ...
}

 

๊ทธ๋ฆฌ๊ณ  ๊ฐ ์ƒํƒœ ์•ˆ์—์„œ ๊ฒฝ๊ด€์ด ํ•ด์•ผํ•  ๊ธฐ๋Šฅ๋“ค์„ ์‹คํ–‰ํ•˜๋Š” ๋กœ์ง์„ ๋งŒ๋“ค์–ด์ฃผ๋ฉด ๋๋‹ค. ํ•˜๋‚˜ํ•˜๋‚˜ ๋‹ค ์ ์–ด๊ฐ€๋ฉฐ ์„ค๋ช…ํ•˜์ž๋‹ˆ, ๊ธ€์ด ๋„ˆ๋ฌด ๊ธธ์–ด์งˆ ๊ฒƒ ๊ฐ™์•˜๊ณ  ์‰ฝ๊ฒŒ ์„ค๋ช…ํ•˜๊ธฐ์—๋„ ์กฐ๊ธˆ ๋ฒ…์ฐจ๋ณด์—ฌ์„œ ๊ฐ„๋‹จํžˆ ์ ์—ˆ๋‹ค.

 

Enemy.cs์™€ ์œ„ ์ƒํƒœ ํด๋ž˜์Šค๋“ค์˜ ๋‚ด๋ถ€ ๋‚ด์šฉ๋“ค์€ ์ƒ๋žตํ•œ๋‹ค. /*  ์ž์„ธํ•œ ๋‚ด๋ถ€ ์ฝ”๋“œ๋Š” ๋‚˜์ค‘์— ์ด ๊ธ€์—๋‹ค๊ฐ€ ๊นƒํ—ˆ๋ธŒ ์ฃผ์†Œ๋ฅผ ์˜ฌ๋ฆฌ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.  */

๊ทธ๋ž˜๋„ ํƒ์ง€ ์ฝœ๋ผ์ด๋”์™€ ์ถ”๊ฒฉ ์ฝœ๋ผ์ด๋” ๋กœ์ง์„ ๋‹ด๋‹นํ•˜๋Š” Detect.cs์™€ Chase.cs ์ •๋„๋Š” ์ ์„ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค.

 

 

 

Detect.cs

 

Enemy ํด๋ž˜์Šค ๋‚ด๋ถ€์— StateMachine ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด๋†“์€ ๊ฒŒ ์žˆ๋‹ค. ๊ทธ๊ฑธ ์ฐธ๊ณ ํ•˜์—ฌ ๋ด์ฃผ๊ธธ ๋ฐ”๋ž€๋‹ค.

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

public class Detect : MonoBehaviour
{

    public Enemy enemy;

    private void OnTriggerEnter2D(Collider2D collision)
    {
        Find(collision);
    }

    private void OnTriggerStay2D(Collider2D collision)
    {
        Find(collision);
    }


    private void Find(Collider2D collision)
    {
        if (collision != null)
        {
            SujiController suji = collision.GetComponent<SujiController>();
            bool findSuji = collision.gameObject.CompareTag("Player") && !suji.IsHiding;

            if (findSuji && enemy.StateMachine.currentState != ChaseState.GetInstance())
            {
                enemy.StateMachine.SetState(enemy, FeelStrange.GetInstance());
            }
        }
    }
}

 

๊ฒฝ๊ด€์ด ์ถ”๊ฒฉ ์ƒํƒœ์ผ ๋•Œ๋Š” ๋’ค๋Œ์•„๋ณผ ํ•„์š”๊ฐ€ ์—†๊ณ , ํ”Œ๋ ˆ์ด์–ด ์บ๋ฆญํ„ฐ๊ฐ€ ์ˆจ์ง€ ์•Š์•˜์„ ๋•Œ ์ž‘๋™ํ•ด์•ผ ํ•œ๋‹ค. ํƒœ๊ทธ๋ž‘ ๋ ˆ์ด์–ด ์ค‘์— ์–ด๋–ค ๊ฑธ๋กœ ํ”Œ๋ ˆ์ด์–ด๋ฅผ ๊ฐ๋ณ„ํ•ด๋‚ผ๊นŒ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ ์šฐ์„  ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.

 

OntriggerStay2D()์—๋„ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ๋„ฃ์–ด์คŒ์œผ๋กœ์จ, ๋ฒ„๊ทธ๋ฅผ ์ˆ˜์ •ํ–ˆ๋‹ค. ๊ฒฝ๊ด€ ๋’ค์— ๊ฐ€์„œ ์ˆจ๊ณ  ๋‹ค์‹œ ๋‚˜์™€์„œ ๋’ค์— ๊ฐ€๋ฉด ์ž‘๋™ํ•˜์ง€ ์•Š๋Š” ๋ฒ„๊ทธ๊ฐ€ ์žˆ์—ˆ๋‹ค. 

 

 

Chase.cs

 

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

public class Chase : MonoBehaviour
{
    public Enemy enemy;


    private void OnTriggerEnter2D(Collider2D collision)
    {
        ChaseTarget(collision);
    }

    private void OnTriggerStay2D(Collider2D collision)
    {
        ChaseTarget(collision);
    }

    private void ChaseTarget(Collider2D collision)
    {
        bool findSuji = collision != null && collision.gameObject.CompareTag("Player");

        if (findSuji)
        {
            SujiController suji = collision.gameObject.GetComponent<SujiController>();
            bool isNotHide = suji != null && !suji.IsHiding;


            if (isNotHide)
            {
                // ์ˆ˜์ง€ ์ถ”๊ฒฉ์„ ์‹œ์ž‘
                ChaseState state = ChaseState.GetInstance();
                state.SetTarget(collision.gameObject.transform);
                enemy.StateMachine.SetState(enemy, state);
            }
        }
    }
}

 

๋น„์Šทํ•œ ๊ตฌ์„ฑ์ด๋ผ์„œ ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์šธ ๊ฒƒ์œผ๋กœ ์ƒ๊ฐ๋œ๋‹ค. ์ฐจ์ด์ ์ด๋ผ๋ฉด ์ถ”๊ฒฉ์„ ์‹œ์ž‘ํ•ด์•ผ ํ•˜๋ฏ€๋กœ, ํƒ€๊ฒŸ ๋Œ€์ƒ์„ ์ง€์ •ํ•ด์ฃผ๋Š” ๋ถ€๋ถ„์ด ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

 

 

์™„์„ฑ

 

๋ฒ„๊ทธ๋„ ๋งŽ์ด ๋‚˜์˜ค๊ณ , ์ƒˆ๋กœ์šด ๊ตฌ์กฐ ์„ค๊ณ„์— ๋„์ „ํ•ด๋ณด๋ฉฐ ํ•œ ๊ฑฐ๋ผ ํ•˜๋ฃจ์ข…์ผ ์ฝ”๋”ฉํ•˜๊ธฐ๋„ ํ–ˆ๋‹ค. ๊ฝค๋‚˜ ์–ด๋ ต๊ณ  ํž˜๋“ค์—ˆ๋˜ ๋ถ€๋ถ„์ธ๋ฐ ๊ทธ๋ž˜๋„ ์ž˜ ๋‚˜์™€์ค˜์„œ ๋„ˆ๋ฌด ๊ธฐ๋ปค๋‹ค.

 

๊ณผ์ •์„ ์ „๋ถ€ ์ ๊ณ  ์‹ถ์—ˆ์œผ๋‚˜, ๋‚ด์šฉ์ด ๋งค์šฐ ๊ธธ์–ด์งˆ ๊ฒƒ ๊ฐ™์•„์„œ ์ด๋ ‡๊ฒŒ ๋ฐ–์— ์„ค๋ช…ํ•˜์ง€ ๋ชปํ•œ ๋ถ€๋ถ„์ด ๋งค์šฐ ์•„์‰ฌ์› ๋‹ค.

๊ฒฝ๊ด€์ด ๋’ค๋Œ์•„ ๋ดค์„ ๋•Œ, ์—†์„ ๊ฒฝ์šฐ ๊ทธ๋ƒฅ ๋‹ค์‹œ ๊ฐ€๋Š” ์˜์ƒ๋„ ์ฐ๊ณ  ์‹ถ์—ˆ๋Š”๋ฐ ์ข€์ฒ˜๋Ÿผ ๊ฐ์ด ์ž˜ ๋‚˜์˜ค์ง€ ์•Š์•„ ํฌ๊ธฐํ–ˆ๋‹ค....ใ…Žใ…Ž

 

๊ฒฝ๊ด€์ด ๋’ค๋Œ์•„๋ดค๋Š”๋ฐ, ๋“ค์ผฐ์„ ๊ฒฝ์šฐ (์ถ”๊ฒฉ ๊ตฌํ˜„ ์•ˆ ํ•œ ๋ฒ„์ „)

 

๊ฒฝ๊ด€์—๊ฒŒ ์ถ”์  ๋‹นํ•˜๋Š”๋ฐ, ์ˆจ์—ˆ์„ ๊ฒฝ์šฐ

 

๊ฒฝ๊ด€์—๊ฒŒ ์ถ”์  ๋‹นํ•˜๋Š” ๋ถ€๋ถ„ (๊ฒŒ์ž„ ์˜ค๋ฒ„ ์ฒ˜๋ฆฌ๋Š” ์•„์ง ๋งŒ๋“ค์ง€ ์•Š์Œ)

 

728x90
๋ฐ˜์‘ํ˜•