[TIL] 51 SavePosition โญโญ

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , ,


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๊ฐ€ ๋ฐ”๋€Œ๊ฒŒ ํ• ๋ ค๊ณ  ํ–ˆ๋‹ค, ํ”Œ๋ ˆ์ด์–ด์— ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋‹ฌ์•˜๋Š”๋ฐ ๊ทธ๋Ÿผ ์”ฌ์„ ์ด๋™ํ•  ๋•Œ ์ด ์Šคํฌ๋ฆฝํŠธ๋Š” ํŒŒ๊ดด๋˜๋ฉด ์•ˆ๋œ๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋๊ณ , ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ ์”ฌ๋ณ„ ์Šคํƒ€ํŠธ ํฌ์ง€์…˜์„ ๋ฐ”๊พธ๊ธฐ๋กœ ํ–ˆ๋‹ค.








์žก๋‹ด,์ •๋ฆฌ




์ฐธ๊ณ  : ์œ ๋‹ˆํ‹ฐ TOP




๐Ÿ“”

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ