- Home /
Saving Player Position between scenes?
Hi, I am currently working on building a 2d sprite based RPG game. So far I've managed to solve any issues that have come up, but I've been working at this one for nearly a week and I am stumped.
The idea is that there is a player sprite you move around the over-world, and whenever it collides with an enemy sprite, a separate battle scene is loaded for a traditional 2d RPG battle, like those in some of the older Final Fantasy games. I can load my battle scene just fine, but whenever the battle ends and the scene closes, the player sprite returns to its original starting position in the over-world. I have two prefabs for the player, one for the player in the over-world and one for the player in the battle scene, since the battle and the over-world require very different scripts for the player to operate. I also have an object that I have named "God" that has a "DoNotDestroyOnLoad" function attached to it, making it persistent between scenes. This object tracks things such as player HP and other stats, and manages some other stuff as well.
What I would like is the game to save the position of the player sprite when it enters the battle, and then after the battle is complete reload the scene with the player at this position. Is this something that can be done with a binary formatter, (which is what I am using to save my data such as player HP and all that) or is there another way that I can do this? I'm honestly not sure what scripts to attach here to reference this, so I'm just going to kind of leave this open ended.
I really appreciate any help that I get in response to this question, thanks in advance to anybody who helps me solve this problem! :)
Answer by CheetahSpeedLion · Jul 27, 2017 at 03:32 AM
I think you could use playerprefs for that. Also I think you could use DontDestroyOnLoad on your player object and hide it in the battle scene, keep it in the background, then activate it again when you switch back.
Hope that helps, Areleli Games
The DontDestroyOnLoad idea is pretty interesting, I'll look into it to see if I can figure out something with that. Playerprefs could help solve the problem, but what I am wondering is whether or not the same thing could be achieved with a binary formatter. (I've tried that and it didn't really work)
You've definitely given me some stuff to think about, though if anybody else has a suggestion please let me know, I'll test this stuff out tomorrow and get back to you to let you know how it works. Thanks!
Your answer
Follow this Question
Related Questions
Making an image appear from a different script 0 Answers
Does anyone know how to look into this sort of thing? (Desciption) 3 Answers
How To Put 2 Panel in 1 scene? 2 Answers
Multiple Cars not working 1 Answer