I have a checkpoint system that works, it saves the coordinates, but when the player passes the level or scene, the next scene loads in the same position as the last level.
I tried to do a
PlayerPrefs.DeleteAll();
but I have a scene menu that unlocks scenes every time you complete a level and this PlayerPrefs.DeleteAll();
deleted the whole list of passed levels.
How do I delete the Checkpoint information for next scene to load in the right place.
This script is in the player script:
private void Start()
{
Rigidbody2D = GetComponent<Rigidbody2D>();
Animator = GetComponent<Animator>();
Advertisement.Initialize(gameId);
if (PlayerPrefs.GetFloat("checkpointX") != 0)
{
transform.position=(new Vector2(PlayerPrefs.GetFloat("checkpointX"), PlayerPrefs.GetFloat("checkpointY")));
}
}
public void recheadCheckPoint(float x, float y)
{
PlayerPrefs.SetFloat ("checkpointX",x);
PlayerPrefs.SetFloat ("checkpointY",y);
}
So, then it takes the info to a CheckPointScript:
public class CheckPoint : MonoBehaviour
{
private Animator Animator;
public AudioClip Cat;
private void Start()
{
Animator = GetComponent<Animator>();
}
private void OnTriggerEnter2D(Collider2D collision)
{
DixonMovement Dixon = collision.GetComponent<DixonMovement>();
CheckPoint checkpoint = collision.GetComponent<CheckPoint>();
if (Dixon != null)
{
collision.GetComponent<DixonMovement>().recheadCheckPoint(transform.position.x, transform.position.y);
Animator.SetTrigger("Duerme");
Camera.main.GetComponent<AudioSource>().PlayOneShot(Cat);
}
if (checkpoint != null)
{
collision.GetComponent<DixonMovement>().recheadCheckPoint(transform.position.x, transform.position.y);
Animator.SetTrigger("Duerme");
Camera.main.GetComponent<AudioSource>().PlayOneShot(Cat);
}
}
Then I have the Scene Controller that allows the player to load the new unlocked levels.
public void levelToLoad(int level)
{
SceneManager.LoadScene(level);
}
How do I delete the recheadCheckPoint(float x, float y) without affecting the other PlayerPrefs to start a new level?
CodePudding user response:
I have a checkpoint system that works, it saves the coordinates, but when the player passes the level or scene, the next scene loads in the same position as the last level.
Instead of deleting the checkpoint data you could also save id of the level the checkpoint resides in. Then when loading the next level you could check if the checkpoint resides in the current level and if not then replace it with the start checkpoint for current level.
Alternatively instead of position you could save the checkpoint id and figure out the spawn position by finding the matching checkpoint and getting the start position from that or using default one if none is found.
Also if reaching the checkpoint saves the current position of the player instead of one determined by the checkpoint, there's a risk of player starting in disadvantageous position depending on the game. I.E a position where player gets instantly shot or spotted by enemies.
CodePudding user response:
I think what you are looking for would be PlayerPrefs.DeleteKey
public static void ResetCheckpoint()
{
PlayerPrefs.DeleteKey("checkpointX");
PlayerPrefs.DeleteKey("checkpointY");
}
Btw for your loading you are currently getting the value twice! Instead you could rather use PlayerPrefs.HasKey
if (PlayerPrefs.HasKey("checkpointX"))
{
transform.position = new Vector2(PlayerPrefs.GetFloat("checkpointX"), PlayerPrefs.GetFloat("checkpointY"));
}