728x90
📌해당 포스팅은 누구나 할 수 있는 유니티 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); //문 열기
}
}
}
}
✅실행 결과
728x90
'🎮 취미 > 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 |