- Home /
How would I go about keeping my player object moving after a scene change?
I have a scene change in my prototype but every time the second scene is loaded, the player stops moving and you have to repress whichever direction you were holding, How would I keep the player moving in the same direction after a scene change? Should I use PlayerPrefs? Any help would be much appreciated.
You could use another script (for example called 'global') which stores your current direction. Whenever your player changes your direction, set the current direction in your 'global'-script.
Then when you load scene2, in your Start()-function load the direction-var of your 'global'-script. Simply let the player walk in this direction until there is given another input to change the direction.
that would work, I would further simplify it by using a scriptable object and to save the player's velocity in there, then when you load a new scene you apply that velocity to the player.
Answer by SGymme · Jul 04, 2021 at 02:45 AM
Just save the velocity in a PlayerPrefs variable and apply that in your second scene on start
Answer by Jonas47 · Jul 06, 2021 at 11:38 AM
We need to save the data, transition the scene, and then load the data back. This assumes you have placed the same Player object (preferably a Prefabbed First, go to your first scene, and create a new empty Game Object.
Answer by UnityPlum · Oct 20, 2021 at 08:58 PM
If you mean like when you change scene the player is still moving whilst the scene changes and if you change scene and go back the player position will have moved whilst in the other seen, you can't exactly do that, but there is a way you can kinda replicate it.
You can do this with the Physics.Simulate(Time.deltaTime) function. what you can do is have a STATIC class that has a STATIC int variable called framesPassed
and what you can do is when you reload the seen you can have a function that is called and in that function go
Physics.autoSimulation = false;
for(int i = 0; i < StaticClassFoo.framesPassed; i++) {
Physics.Simulate(Time.fixedDeltaTime);
}
Physics.autoSimulation = true;
And then while in the other seen constantly add to framesPassed. And to fix the int stacking, every time you load up scene 2, reset the framesPassed to 0.
Your answer
Follow this Question
Related Questions
Why does WASD move my player quicker than with a joystick? 1 Answer
Why am I only allowed to trigger my dash script once? 2 Answers
Ball Rolling Forever 0 Answers
I need help with a CharacterMotor script error in Unity5. This is formatted in Javascript. 0 Answers
Help with player movement and adding force to a ball. 1 Answer