using UnityEngine;
using UnityEngine.SceneManagement;
public class TransferScene3 : MonoBehaviour
{
public string transferMapName;//이동할 씬의 이름
public string nextScenePoint;//씬 이동 후 MapInPoint
private MovingObjcet thePlayer;
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.name == "Player")
{
SceneManager.LoadScene(transferMapName);
SceneManager.sceneLoaded += OnSceneLoaded;
}
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
SceneManager.sceneLoaded -= OnSceneLoaded;
MoveingMapPlayer();
}
void MoveingMapPlayer()
{
thePlayer = FindAnyObjectByType<MovingObjcet>();
GameObject nextPoint = GameObject.Find(nextScenePoint);
if (thePlayer != null && nextPoint != null)
{
thePlayer.transform.position = new Vector3(nextPoint.transform.position.x, nextPoint.transform.position.y, thePlayer.transform.position.z);
}
else
{
Debug.Log("실패했습니다");
}
}
}
첫번째 함수는 이동할 씬의 이름이고
두번째 함수는 감지할 빈오브젝트의 string변수로 문자로 받음
세번째 함수는 플레이어
void MoveingMapPlayer()
{
thePlayer = FindAnyObjectByType<MovingObjcet>();
GameObject nextPoint = GameObject.Find(nextScenePoint);
if (thePlayer != null && nextPoint != null)
{
thePlayer.transform.position = new Vector3(nextPoint.transform.position.x, nextPoint.transform.position.y, thePlayer.transform.position.z);
}
else
{
Debug.Log("실패했습니다");
}
}
이제 평범하게 플레이어 스크립트 받아오고, next 포인트 받아와서
!= null로 오브젝트가 있나 없나 체크 후 이동시킨다
그러면 성공!
성공해서 씬 이동후 따로 구분해 놓은 빈 오브젝트로 이동
'쯔꾸르식 유니티 게임 공부' 카테고리의 다른 글
10.5 추가설명 (0) | 2025.01.14 |
---|---|
10.NPC구현하기 (0) | 2025.01.10 |
6.개량 씬과 씬 이동하면서 위치 지정하기 (0) | 2025.01.05 |
09.BGM매니져(배경음) (1) | 2025.01.03 |
08.오디오 메니져 만들기 (0) | 2025.01.03 |