Setting Player Position on scene loading

Hi,
I have an issue that drives me crazy. I am creating a LEGO game using the LEGO microgame template.
I created a simple checkpoint system with a static class where I keep the last checkpoint position that the player passed.
My static class looks like:

public static class SceneState
{
     public static Vector3 LastCheckpoint;
}

When the player passes a checkpoint I save the position
My Checkpoint class:

    public class CheckPoint : MonoBehaviour
    {

        private void OnTriggerEnter(Collider other)
        {
            if (other.CompareTag("Player"))
            {
                SceneState.LastCheckpoint = new Vector3(transform.position.x, other.transform.position.y, transform.position.z);
            }
        }
    }

When the player dies and the scene is reloaded I set the Player position to the saved position

private void SceneManager_sceneLoaded(Scene scene, LoadSceneMode loadSceneMode)
{
    if (SceneState.LastCheckpoint != Vector3.zero)
    {
        GameObject player = GameObject.FindGameObjectWithTag("Player");
        player.transform.position = SceneState.LastCheckpoint;
    }
}

When I debug, I see that the position is correctly saved when the player passes a checkpoint and the position is correctly set to the last checkpoint position when the scene is loaded. So it seems that it should work.

Now when playing the game sometimes the player is correctly placed on the last checkpoint but sometimes it isn’t and the player is set back to the initial spawn position of the scene… And this behaviour is random. I cannot figure how this is possible.

Anyone has the same issue?
Anyone knows how to fix?
Thanks in advance.

Does your player have a CharacterController?

Yes the player has a CharacterController.

Try disabling the CC, moving, then enabling Cc

1 Like

That did the trick! Thank you very much! :slight_smile: