Super Kawaii Cute Cat Kaoani
๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
{Unity}

์œ ๋‹ˆํ‹ฐ ์Šคํ„ฐ๋”” - ํƒ‘๋ทฐ ์•ก์…˜ ๊ฒŒ์ž„ ์—…๊ทธ๋ ˆ์ด๋“œ ํ•˜๊ธฐ๐Ÿ†™

by wonee1 2024. 11. 22.
728x90

๐Ÿ“Œํ•ด๋‹น ํฌ์ŠคํŒ…์€ ๋ˆ„๊ตฌ๋‚˜ ํ•  ์ˆ˜ ์žˆ๋Š” ์œ ๋‹ˆํ‹ฐ 2D ๊ฒŒ์ž„ ์ œ์ž‘์ฑ…์˜ ์‹ค์Šต ๋‚ด์šฉ์„ ํ† ๋Œ€๋กœ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค. 

 

์ฑ… ๋งํฌ๐Ÿ”ฝ

https://www.yes24.com/Product/Goods/112941439

 

๋ˆ„๊ตฌ๋‚˜ ํ•  ์ˆ˜ ์žˆ๋Š” ์œ ๋‹ˆํ‹ฐ 2D ๊ฒŒ์ž„ ์ œ์ž‘ - ์˜ˆ์Šค24

๊ฒŒ์ž„ ๊ฐœ๋ฐœ, ์œ ๋‹ˆํ‹ฐ, ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ชจ๋‘ ์ฒ˜์Œ์ธ ์‚ฌ๋žŒ์„ ์œ„ํ•œ ๋‹จ ํ•œ ๊ถŒ์˜ ์ฑ… ์ค‘ํ•™๊ต ์ˆ˜์ค€์˜ ์˜์–ด์™€ ์ˆ˜ํ•™, ๊ทธ๋ฆฌ๊ณ  ‘๊ฒŒ์ž„์„ ์ข‹์•„ํ•˜๊ณ  ๊ฒŒ์ž„์„ ๋งŒ๋“ค๊ณ  ์‹ถ๋‹ค’๋Š” ๋งˆ์Œ๋งŒ ์žˆ๋‹ค๋ฉด ๋ˆ„๊ตฌ๋‚˜ ์ฆ๊ฒ๊ฒŒ ์œ ๋‹ˆํ‹ฐ ์‚ฌ

www.yes24.com

 


 

์”ฌ์—์„œ ์”ฌ์œผ๋กœ ์ด๋™ํ•˜๊ธฐ

 

 

๐Ÿ’ ์ถœ์ž…๊ตฌ ๊ฒŒ์ž„ ์˜ค๋ธŒ์ ํŠธ์™€ ์Šคํฌ๋ฆฝํŠธ ๋งŒ๋“ค๊ธฐ

 

  1. ๋นˆ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์”ฌ์— ๋ฐฐ์น˜ ํ›„ Exit๋กœ ์„ค์ •ํ•œ ๋‹ค์Œ tag ์„ค์ •๋„ ์ง„ํ–‰ํ•ด์ค€๋‹ค.
  2. Circle Colider 2D๋ฅผ ์–ดํƒœ์น˜ ํ•œ ๋‹ค์Œ is Trigger๋ฅผ ์ฒดํฌํ•œ๋‹ค.
  3. RoomManager ํด๋”์— Exit ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋งŒ๋“  ํ›„ Exit ์˜ค๋ธŒ์ ํŠธ์— ์–ดํƒœ์น˜

 

 

 

Exit.cs

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

//์ถœ์ž…๊ตฌ ์œ„์น˜
public enum ExitDirection
{
    right,  //์˜ค๋ฅธ์ชฝ
    left,   //์™ผ์ชฝ
    down,   //์•„๋ž˜์ชฝ
    up,     //์œ„์ชฝ
}

public class Exit : MonoBehaviour
{
    public string sceneName = "";   //์ด๋™ํ•  ์”ฌ ์ด๋ฆ„
    public int doorNumber = 0;      //๋ฌธ ๋ฒˆํ˜ธ
    public ExitDirection direction = ExitDirection.down;//๋ฌธ์˜ ์œ„์น˜

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "Player")
        {
            RoomManager.ChangeScene(sceneName, doorNumber);
        }
    }
}

 

 

 

 

 

๐Ÿ’  ๋ฐฉ์„ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒŒ์ž„ ์˜ค๋ธŒ์ ํŠธ ๋งŒ๋“ค๊ธฐ

 

  1. RoomManger ์ด๋ฆ„์˜ ๋นˆ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค
  2. RoomManger ์Šคํฌ๋ฆฝํŠธ๋ฅผ RoomManager ํด๋”์— ๋„ฃ์€ ๋‹ค์Œ ์˜ค๋ธŒ์ ํŠธ์— ์–ดํƒœ์น˜ํ•œ๋‹ค.

 

RoomManger.cs

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

public class RoomManager : MonoBehaviour
{
    // static ๋ณ€์ˆ˜
    public static int doorNumber = 0;   //๋ฌธ ๋ฒˆ

    // Start is called before the first frame update
    void Start()
    {
        //ํ”Œ๋ ˆ์ด์–ด ์บ๋ฆญํ„ฐ ์œ„์น˜
        //์ถœ์ž…๊ตฌ๋ฅผ ๋ฐฐ์—ด๋กœ ์–ป๊ธฐ
        GameObject[] enters = GameObject.FindGameObjectsWithTag("Exit");
        for (int i = 0; i < enters.Length; i++)
        {
            GameObject doorObj = enters[i]; //๋ฐฐ์—ด์—์„œ ๊บผ๋‚ด๊ธฐ
            Exit exit = doorObj.GetComponent<Exit>();   //Exit ํด๋ž˜์Šค ๋ณ€์ˆ˜
            if (doorNumber == exit.doorNumber)
            {
                //==== ๊ฐ™์€ ๋ฌธ  ๋ฒˆํ˜ธ ====
                //ํ”Œ๋ ˆ์ด์–ด ์บ๋ฆญํ„ฐ๋ฅผ ์ถœ์ž…๊ตฌ๋กœ ์ด๋™
                float x = doorObj.transform.position.x;
                float y = doorObj.transform.position.y;
                if (exit.direction == ExitDirection.up)
                {
                    y += 1;
                }
                else if (exit.direction == ExitDirection.right)
                {
                    x += 1;
                }
                else if (exit.direction == ExitDirection.down)
                {
                    y -= 1;
                }
                else if (exit.direction == ExitDirection.left)
                {
                    x -= 1;
                }
                GameObject player = GameObject.FindGameObjectWithTag("Player");
                player.transform.position = new Vector3(x, y);
                break;  //๋ฐ˜๋ณต๋ฌธ ๋น ๋‚˜์˜ค๊ธฐ
            }
        }
    }

    // Update is called once per frame
    void Update()
    {

    }

    //์”ฌ ์ด๋™
    public static void ChangeScene(string scnename, int doornum)
    {
        doorNumber = doornum;   //๋ฌธ ๋ฒˆํ˜ธ๋ฅผ static ๋ณ€์ˆ˜์— ์ €์žฅ

        SceneManager.LoadScene(scnename);   
    }
}

 

 

๐Ÿ’  ์ถœ์ž…๊ตฌ ๋ฐฐ์น˜ํ•˜๊ธฐ , ๋ฌธ ๋งŒ๋“ค๊ธฐ

 

 

 

 

 

 

 

 

   1. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค. ๊ทธ ๋‹ค์Œ Exit ์œ„์น˜๋ฅผ ์กฐ์ ˆํ•ด์ค๋‹ˆ๋‹ค.

 

 

 

    2. ๋ฌธ์€ Item ํด๋”๋ฅผ ๋งŒ๋“  ๋‹ค์Œ ๊ทธ ์•ˆ์— ์ด๋ฏธ์ง€๋ฅผ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

    3. ํ”ฝ์…€์€ 32๋กœ ์„ค์ •ํ•ด์ฃผ๊ณ  Point(no filter)๋กœ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค

    4. Door ์Šคํฌ๋ฆฝํŠธ๋ฅผ Item ํด๋”์— ๋งŒ๋“ค๊ณ  ์”ฌ ๋ทฐ์— ๋ฐฐ์น˜๋œ door์— ์–ดํƒœ์น˜ํ•ฉ๋‹ˆ๋‹ค

 

 

Door.cs

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

public class Door : MonoBehaviour
{
    public int arrangeId = 0;       //์‹๋ณ„์— ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ’

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Player")
        {
            //์—ด์‡ ๋ฅผ ๊ฐ€์ง€๊ณ ์žˆ์œผ๋ฉด
            if (ItemKeeper.hasKeys > 0)
            {
                ItemKeeper.hasKeys--;       //์—ด์‡ ๋ฅผ ํ•˜๋‚˜ ๊ฐ์†Œ
                Destroy(this.gameObject);   //๋ฌธ ์—ด๊ธฐ
            }
        }
    }
}

 

 

 

 

โœ…์‹คํ–‰ ๊ฒฐ๊ณผ 

 

 

 

 

728x90