Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Syaheera26 · Jun 25, 2021 at 06:26 AM · 3d2d gamegamerespawnfps controller

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");
         }

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_HTr7mOeTIRzvVA · Jun 25, 2021 at 08:35 AM 0
Share

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?
     }
 

@Syaheera26

avatar image Syaheera26 unity_HTr7mOeTIRzvVA · Jun 25, 2021 at 03:35 PM 0
Share

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!

avatar image unity_HTr7mOeTIRzvVA Syaheera26 · Jun 26, 2021 at 02:26 AM 0
Share
  1. 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.

  2. You should separate saving and loading. Saving is when you're about to leave the Main scene, Loading is when entering the Main scene.

  3. 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

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Syaheera26 · Jun 26, 2021 at 06:13 AM 0
Share

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!

avatar image ziyadedris · Jun 26, 2021 at 08:59 AM 0
Share

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 ( :

avatar image Syaheera26 ziyadedris · Jun 26, 2021 at 01:43 PM 0
Share

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?

avatar image
0

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;}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Syaheera26 · Jun 26, 2021 at 06:22 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

212 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Collider Intersects with Terrain 0 Answers

How to make my space ship shooting (2D Game)? 0 Answers

Physic Based FPS Movement,Fps Movement Physic Based 0 Answers

Whats The difference? 1 Answer

counting score after destroying game object 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges