[TIL] 51 SavePosition โญโญ
์นดํ ๊ณ ๋ฆฌ: Til
SavePosition
SavePosition
ย ย [o] ์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ - 52
ย ย [o] ๋ค๋ฅธ๋ฐ ๊ฐ์ ๋ฃ๊ธฐ
ย ย [x] ์ฌํ์ฃผ์ฐจ ๊ฐ์ ๋ฃ๊ธฐ.
SavePoint
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SavePoint : MonoBehaviour
{
private Vector3 _startPoint; // ์์์์น ์ค์ .
private Vector3 _firstStartPoint = new Vector3(0,50,50); // 1์คํ
์ด์ง ์์์์น ์ค์ .
private Vector3 _SecondStartPoint = new Vector3(94, 0, 15); // 2์คํ
์ด์ง ์์์์น ์ค์ .
private Vector3 _savePoint = Vector3.zero; // ์ ์ฅ์์น ์ค์ .
private HealthSystem HealthSystem;
private PlayerRagdollController _playerRagdollController;
private void Awake()
{
_playerRagdollController = GetComponent<PlayerRagdollController>();
HealthSystem = GetComponent<HealthSystem>();
}
private void Start()
{
Scene scene = SceneManager.GetActiveScene();
sceneCheck(scene);
_savePoint = _startPoint;
HealthSystem.OnDied += revive;
//SceneManager.sceneLoaded += LoadedsceneEvent;
}
public void revive()
{
StartCoroutine(ReStartCo());
}
private void Update()
{
if(Input.GetKeyDown(KeyCode.R))
{
StartCoroutine(ReStartCo());
}
checkSaveBoard();
Debug.DrawRay(transform.position, Vector3.down, Color.red, 0.3f);
}
/*private void LoadedsceneEvent(Scene scene, LoadSceneMode arg1)
{
_player = GameObject.FindGameObjectWithTag("Player");
sceneCheck(scene);
_savePoint = _startPoint;
}*/
private void checkSaveBoard()
{
RaycastHit _hit;
if (Physics.Raycast(transform.position, Vector3.down, out _hit, 1,4))
{
if (_hit.transform.CompareTag("SaveBoard"))
{
Debug.Log("์ธ์ด๋ธ๋ณด๋");
_savePoint = _hit.transform.position + Vector3.up;
//์ธ์ด๋ธ ์ดํํธ
Destroy(_hit.collider.gameObject, 1f);
}
}
}
public IEnumerator ReStartCo()
{
_playerRagdollController.SetRagdollState(true);
yield return new WaitForSeconds(5.1f);
gameObject.transform.position = _savePoint;
_savePoint = _startPoint;
}
private void sceneCheck(Scene scene)
{
if (scene.name == "KDH_Obstacle")
{
_startPoint = _firstStartPoint;
}
else if (scene.name == "99.BJH")
{
_startPoint = _SecondStartPoint;
}
else
{
_startPoint = Vector3.zero;
}
}
}
SceneManager.sceneLoaded += LoadedsceneEvent;
private void LoadedsceneEvent(Scene scene, LoadSceneMode arg1)
{
_player = GameObject.FindGameObjectWithTag(โPlayerโ);
sceneCheck(scene);
_savePoint = _startPoint;
}
์ฌ ์ด๋์ startpoint๊ฐ ๋ฐ๋๊ฒ ํ ๋ ค๊ณ ํ๋ค, ํ๋ ์ด์ด์ ์คํฌ๋ฆฝํธ๋ฅผ ๋ฌ์๋๋ฐ ๊ทธ๋ผ
์ฌ์ ์ด๋ํ ๋ ์ด ์คํฌ๋ฆฝํธ๋ ํ๊ดด๋๋ฉด ์๋๋ค๊ณ ์๊ฐ์ด ๋๊ณ , ๋ค๋ฅธ ๋ฐฉ๋ฒ์ผ๋ก ์ฌ๋ณ ์คํํธ ํฌ์ง์
์ ๋ฐ๊พธ๊ธฐ๋ก ํ๋ค.
๋๊ธ๋จ๊ธฐ๊ธฐ