using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CheckPoint : MonoBehaviour
{
[SerializeField] GameObject player;
[SerializeField] List<GameObject> checkpoints;
[SerializeField] Vector3 vectorPoint;
[SerializeField] float dead;
public Text coinTxt;
public int coins;
private void Start()
{
// Загружаем сохраненные данные при старте
LoadGame();
coinTxt.text = coins.ToString();
}
private void Update()
{
if(player.transform.position.y < -dead)
{
player.transform.position = vectorPoint;
}
}
private void OnTriggerEnter(Collider check)
{
// Сохраняем позицию чекпоинта
vectorPoint = player.transform.position;
// Удаляем монету из мира
if (check.gameObject.CompareTag("coin"))
{
coins++;
coinTxt.text = coins.ToString();
Destroy(check.gameObject);
SaveGame(); // Сохраняем состояние игры
}
}
private void SaveGame()
{
PlayerPrefs.SetInt("Coins", coins);
PlayerPrefs.SetString("Checkpoint", vectorPoint.ToString());
PlayerPrefs.Save(); // Сохраняем изменения
}
private void LoadGame()
{
coins = PlayerPrefs.GetInt("Coins", 0); // 0 - значение по умолчанию
string checkpointString = PlayerPrefs.GetString("Checkpoint", vectorPoint.ToString());
vectorPoint = StringToVector3(checkpointString);
}
private Vector3 StringToVector3(string sVector)
{
string[] sArray = sVector.Replace("(", "").Replace(")", "").Split(',');
return new Vector3(float.Parse(sArray[0]), float.Parse(sArray[1]), float.Parse(sArray[2]));
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using static UnityEditor.PlayerSettings;
public class CheckPoint : MonoBehaviour
{
[SerializeField] GameObject player;
[SerializeField] List<GameObject> checkpoints;
[SerializeField] Vector3 vectorPoint;
[SerializeField] float dead;
public Text coinTxt;
public int coins;
private void Start()
{
coins = PlayerPrefs.GetInt("Coins", 0);
coinTxt.text = coins.ToString();
vectorPoint = new Vector3(
PlayerPrefs.GetFloat("CheckpointX", player.transform.position.x),
PlayerPrefs.GetFloat("CheckpointY", player.transform.position.y),
PlayerPrefs.GetFloat("CheckpointZ", player.transform.position.z)
);
player.transform.position = vectorPoint;
}
private void Update()
{
if(player.transform.position.y < -dead)
{
player.transform.position = vectorPoint;
}
}
private void OnTriggerEnter(Collider check)
{
if (check.gameObject.tag == "coin")
{
coins++;
PlayerPrefs.SetInt("Coins", coins);
PlayerPrefs.Save();
coinTxt.text = coins.ToString();
Destroy(check.gameObject);
}
else if (check.gameObject.tag == "checkpoint")
{
vectorPoint = player.transform.position;
PlayerPrefs.SetFloat("CheckpointX", vectorPoint.x);
PlayerPrefs.SetFloat("CheckpointY", vectorPoint.y);
PlayerPrefs.SetFloat("CheckpointZ", vectorPoint.z);
PlayerPrefs.Save();
Destroy(check.gameObject);
}
}
}