- Home /
How to Make a Checkpoint for a 3D game!
Hi
I'm working on a 3D endless runner game and when my player falls off an edge (y < 2), my scene reloads to let the player play again. It works well but I'm trying to add a checkpoint that gets activated after a distance (Z-axis = 2400) has been run past. I'm using the script below to achieve what I want. (I've already grabbed a reference to the "RB" on this script.)
if (SceneManager.GetActiveScene().name == "Level 7") { if (RB.position.z > 2400) { if (RB.position.y < 2) { FindObjectOfType().EndGame2(); } }
Then after taking me to the method "EndGame2" on a new script named "GameManager", it continues:
public GameObject Player;
private void Awake() { Player = GameObject.Find("Player"); DontDestroyOnLoad(gameObject); }
public void EndGame2() { Invoke("Restart2", 4f); }
public void Restart2 ()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
Player.transform.position = new Vector 3 (0, 1, 2400);
}
Now, the main problem is that once the level reloads, the player starts moving form still the point zero on Z-axis (not 2400) and it seems the "DontDestroyOnLoad" doesn't work well. I know I may have had many mistakes. I would appreciate it if someone please helps me with a clear explanation. Thanks in advance.
What 3D movement script are you using? Is t something from the standard assets? In my game, the first person movement script was moving my character back to where it was previously before I loaded it. To test this theory, you can temporarily add Time.timescale = 0; before the loading code. Debug this in unity and see if the player position stays when time is paused. If it reverts, it is most likely your movement script interfering. Also, should be Vector3 rather than Vector 3
You are reloading the scene and setting the player position before it is reloading hence why your player always restarts from the default z pos.
Answer by thomh_unity · Oct 02, 2019 at 07:25 AM
Hi Siavash198,
The Player variable you're modifying in that method is the one from the current scene. If you replace the player when the scene loads, that would be why it seems to not move. You can debug this by using the GetInstanceID(); for example (assuming that Player is a gameObject):
public void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
Debug.Log("What's my player's ID: " + Player.GetInstanceID());
}
void Start()
{
Debug.Log("What's my player's ID at start: " + Player.GetInstanceID());
}
You will then see if the object you're modifying the position on is the same instance as the one you end up with after the scene load.
I hope this helps.
Your answer
Follow this Question
Related Questions
Persistent GameObject Between scene Without Serilization 1 Answer
pass data between scene for game settings? 1 Answer
URP Unity second camera (From other scene) not being detected for stacking 0 Answers
DontDestroyOnLoad reset at certain point 2 Answers
When I reload my Scene, nothing works. I think it's because of a dontDestroyOnLoad gameObject 0 Answers