I am creating a 2D game, now I am trying to make a checkpoint system, upon death the player must return to this position. Created a script and applied it to all checkpoints at the level. The checkpoint is a plate added to it BoxCollider2D and made it trigger.
public class CheckPoint : MonoBehaviour
{
ReloadPosition reloadPosition;
Vector2 pointUpdate;
private void OnTriggerEnter2D(Collider2D coll)
{
if(coll.gameObject.CompareTag("Player"))
{
pointUpdate = new Vector2(transform.position.x, transform.position.y);
}
}
void Start()
{
reloadPosition = GetComponent<ReloadPosition>();
}
}
I also created a script that remembers and returns to the saved position when dropped into water
public class ReloadPosition : MonoBehaviour
{
public float startX;
public float startY;
private void Start()
{
startX = transform.position.x;
startY = transform.position.y + 1;
}
private void OnCollisionEnter2D(Collision2D coll)
{
if(coll.gameObject.CompareTag("water"))
{
ReloadPlayerPosition();
}
}
public void ReloadPlayerPosition()
{
transform.position = new Vector2(startX, startY);
}
}
Tell me how to correctly implement the checkpoint script …
