- Home /
Respawn back at the old position.
Hi! I am a newbie trying to learn creating a game for my fyp. So my game about a maze game (using a fpc) and inside the maze, there consist several checkpoint that triggers when player collide with the checkpoint. The problem right now is that after the player finished inside the checkpoint's scene (2D game) and go back to the main game scene, they always respawn at the exact first place of the starting game. I am trying to make them respawn back near the checkpoint that they have finished. I tried using the save and load player position data but it doesn't work. Please help me. Thank you.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SavePlayerPos : MonoBehaviour
{
public GameObject player;
private void Start()
{
if(PlayerPrefs.GetInt("Saved") == 1 && PlayerPrefs.GetInt("TimeToLoad") == 1)
{
float pX = player.transform.position.x;
float pY = player.transform.position.y;
float pZ = player.transform.position.z;
pX = PlayerPrefs.GetFloat("p_x");
pY = PlayerPrefs.GetFloat("p_y");
pZ = PlayerPrefs.GetFloat("p_z");
player.transform.position = new Vector3(pX, pY, pZ);
PlayerPrefs.SetInt("TimeToLoad", 0);
PlayerPrefs.Save();
}
}
public void PlayerPosSave()
{
PlayerPrefs.SetFloat("p_x", player.transform.position.x);
PlayerPrefs.SetFloat("p_y", player.transform.position.y);
PlayerPrefs.SetFloat("p_z", player.transform.position.z);
PlayerPrefs.SetInt("Saved", 1);
PlayerPrefs.Save();
}
public void PlayerPosLoad()
{
PlayerPrefs.SetInt("TimeToLoad", 1);
PlayerPrefs.Save();
}
}
and this is my script for the object that triggers the changing scene
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ChangeScene : MonoBehaviour
{
SavePlayerPos playerPosData;
void Start()
{
playerPosData = FindObjectOfType<SavePlayerPos>();
}
public void GoBackToGame1()
{
playerPosData.PlayerPosSave();
playerPosData.PlayerPosLoad();
SceneManager.LoadScene("Level 1");
}
Save and Load methods shouldn't be inside "GoBackToGame1()". Saving and loading should only be happen on your Main Scene.
On the startup of your main scene, you should always "load" a position data by default. While "saving" is a public method you can call anytime along with SceneManager.LoadScene.
Example:
SavePlayerPos Script
// call this when exiting the main scene
public void SavePosition(Vector3 newPosition){
PlayerPrefs.SetFloat("p_x", newPosition.x);
PlayerPrefs.SetFloat("p_y", newPosition.y);
PlayerPrefs.SetFloat("p_z", newPosition.z);
}
// call this when entering the scene
public Vector3 LoadPosition(){
var loadedData = new Vector3(
PlayerPrefs.GetFloat("p_x"),
PlayerPrefs.GetFloat("p_y"),
PlayerPrefs.GetFloat("p_z")
);
return loadedData;
}
from Whatever script of the Main Scene
Vector3 defaultPosition; // the default position where the level designer placed the character object
SavePlayerPos playerPosData;
void Start(){
// Load Position
defaultPosition = transform.position;
var loadedPosition = playerPosData.Load();
if(loadedData != Vector3.zero)
transform.position = loadedPosition;
else
transform.position = defaultPosition;
// other
playerPosData = FindObjectOfType<SavePlayerPos>();
}
public void EnterCheckpointScene(){
playerPosData.SavePosition(transform.position);
SceneManager.LoadScene("CheckpointScene");
}
from whatever script of the Checkpoint Scene
public void GoBackToGame1()
{
SceneManager.LoadScene("Level 1"); // is "Level 1" the name of your Main Scene?
}
Hi! thank you for your reply! I am a little confuse with the part on where I should attach the script for SavePlayerPos. Do I need to attach the code to the player? And do I also need to separate both part for entering and exiting the main scene? And for the second script that you have mentioned, is the script also attached to the player? And the second script is about the initial starting position for the player, right?
And yes, Level 1 is the name of the main scene. I wanted to make 2 level for the game with each level consist of several checkpoint.
Sorry for the stupid questions that I asked, I still unable to grab the knowledge in developing and scripting for the game. Thank you again!
You can attach SavePlayerPos to any gameObject you want but i suggest you create an empty game object named "SaveManager" or something related. Put the SavePlayerPos to that to make it a lil bit organized.
You should separate saving and loading. Saving is when you're about to leave the Main scene, Loading is when entering the Main scene.
In my example, that script is attached to the player gameobject. The purpose is to record the last position of the player when leaving the scene, and to initialize the recorded position when entering the scene. @Syaheera26
Answer by ziyadedris · Jun 25, 2021 at 03:54 PM
if you don't want to use playerprefs thing, you can watch this vid https://www.youtube.com/watch?v=ofCLJsSUom0&t=232s
it's also very simple
Hi! I watched this video before but I can't get the concept. Like, in the video shown, they automatically respawn at the checkpoint. But what if I want to respawn at the place after I exited other scene? Thank you for the reply!
but he uses the don't destroy on load method so the object that has the script doesn't get restarted when you restart the scene or open a new scene. also "But what if I want to respawn at the place after I exited other scene? " if you want, you can make the checkpoints not only store a position, you can also make them store a scene by using this code Scene MyScene = SceneManager.GetActiveScene(); SceneManager.LoadScene(MyScene.name);
and if you wanted to save the checkpoint even if the player left the game, you can make a timer and every time this timer hits 0 reset it and use player prefs to store the position and scene I hope you understand ( :
Oh, I will definitely note that down about the timer! Thank you! 1. Scene MyScene = SceneManager.GetActiveScene(); SceneManager.LoadScene(MyScene.name);
related to this code here, should i attach this to the trigger to the other scene? Should I make it like void OnTriggerEnter(Collider other) { Scene MyScene = SceneManager.GetActiveScene(); SceneManager.LoadScene(MyScene.name); SceneManager.LoadScene("Game_2P4"); }
like this?
Answer by logicandchaos · Jun 25, 2021 at 05:24 PM
When you restart the game you have to reset the start position, if you sae the check point position, then set it to the start position. I sometimes use a script that saves the position in OnStart() or OnAwake() and then uses that position when ever the game restarts, that works great when you set up your player in your scene.
Vector3 startPos; Start(){ startPos=transform.position;}
RestartGame(){ transform.position=startPos;}
Thank you for your reply! I have actually wrote down the concept that you mentioned inside the fps controller script but it doesn't work. I'm afraid I probably did it wrongly-
Your answer
Follow this Question
Related Questions
Locking movement to one axis only (3D game) 1 Answer
Question about Background-music in Unity3D 1 Answer
Where can I get FREE 3D models for my game? 1 Answer
Ball goes through the wall 2 Answers