๐ํด๋น ํฌ์คํ ์ ๋๊ตฌ๋ ํ ์ ์๋ ์ ๋ํฐ 2D ๊ฒ์ ์ ์์ฑ ์ ์ค์ต ๋ด์ฉ์ ํ ๋๋ก ์์ฑํ์์ต๋๋ค.
์ฑ ๋งํฌ๐ฝ
https://www.yes24.com/Product/Goods/112941439
๋๊ตฌ๋ ํ ์ ์๋ ์ ๋ํฐ 2D ๊ฒ์ ์ ์ - ์์ค24
๊ฒ์ ๊ฐ๋ฐ, ์ ๋ํฐ, ํ๋ก๊ทธ๋๋ฐ ๋ชจ๋ ์ฒ์์ธ ์ฌ๋์ ์ํ ๋จ ํ ๊ถ์ ์ฑ ์คํ๊ต ์์ค์ ์์ด์ ์ํ, ๊ทธ๋ฆฌ๊ณ ‘๊ฒ์์ ์ข์ํ๊ณ ๊ฒ์์ ๋ง๋ค๊ณ ์ถ๋ค’๋ ๋ง์๋ง ์๋ค๋ฉด ๋๊ตฌ๋ ์ฆ๊ฒ๊ฒ ์ ๋ํฐ ์ฌ
www.yes24.com
์ฌ์์ ์ฌ์ผ๋ก ์ด๋ํ๊ธฐ
๐ ์ถ์ ๊ตฌ ๊ฒ์ ์ค๋ธ์ ํธ์ ์คํฌ๋ฆฝํธ ๋ง๋ค๊ธฐ
- ๋น ์ค๋ธ์ ํธ๋ฅผ ์ฌ์ ๋ฐฐ์น ํ Exit๋ก ์ค์ ํ ๋ค์ tag ์ค์ ๋ ์งํํด์ค๋ค.
- Circle Colider 2D๋ฅผ ์ดํ์น ํ ๋ค์ is Trigger๋ฅผ ์ฒดํฌํ๋ค.
- 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);
}
}
}
๐ ๋ฐฉ์ ๊ด๋ฆฌํ๋ ๊ฒ์ ์ค๋ธ์ ํธ ๋ง๋ค๊ธฐ
- RoomManger ์ด๋ฆ์ ๋น ์ค๋ธ์ ํธ๋ฅผ ๋ง๋ค์ด์ค๋ค
- 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); //๋ฌธ ์ด๊ธฐ
}
}
}
}
โ ์คํ ๊ฒฐ๊ณผ
'{Unity}' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ์ ๋ํฐ ์คํฐ๋ - ๊ฒ์ ๊ฐ๋ฐ ์์ฑ ๊ฐ์ (2) (2) | 2024.11.15 |
|---|---|
| ์ ๋ํฐ ์คํฐ๋ - ๊ฒ์ ๊ฐ๋ฐ ์์ฑ ๊ฐ์ (1) (1) | 2024.11.08 |
| ์ ๋ํฐ ์ฏ๊พธ๋ฅด ๊ฒ์ ๋ง๋ค๊ธฐ 4 (์ธํ๋ฐ) (0) | 2023.07.01 |
| ์ ๋ํฐ ์ฏ๊พธ๋ฅด ๊ฒ์ ๋ง๋ค๊ธฐ 3 (์ธํ๋ฐ) (0) | 2023.07.01 |
| ์ ๋ํฐ ์ฏ๊พธ๋ฅด ๊ฒ์ ๋ง๋ค๊ธฐ 2 (์ธํ๋ฐ) (0) | 2023.07.01 |