- Home /
Input.GetAxis returns 0 after scene load
I currently have 2 scenes. When the player object enters a trigger on the right side of one scene, I load the second scene and the player starts on the left side of it. Basic side scrolling type stuff. I would expect if the player continues to hold down the key to move right, then the player object will continue to move.
If the player only holds the right key down, the player object will continue to move right when the next scene loads. Input.GetAxis("Horizontal") returns a value greater than 0.
The problem is if the player jumps or presses any other button before the next scene loads, then, Input.GetAxis("Horizontal") will only return 0. Is there any work around for this? I would expect the player to be able to jump or perform any other action while moving out of a scene, and still be able to get the input on the next scene.
NOTE: If the player releases the key to move right after the next scene loads and presses it down again, Input.GetAxis("Horizontal") will start working again. But I don't feel as though this should be a requirement.
you could maybe buffer the pressed key, and then reset it on a getButtonUp?
Weird, I just came across this same problem. What did you do to resolve it?
Answer by boomcallister · Jan 07, 2012 at 09:47 PM
Weird, I just came across this same problem. Have you resolved it? What did you do?
I am creating a 2D platformer, so my solution may not work for you if your scenes are very complicated.
Since I do not have Pro (which you can use async scene loading which I believe would solve this problem), this is what I did. It took me some time to code the solution and much testing but it is working perfectly.
1) Every scene has its own folder for storing scene specific prefabs. For example:
/Assets/Resources/Scenes/0_0/
/Assets/Resources/Scenes/0_1/ and so on.
2) All objects in your scene must be prefabs. Any prefabs which can be duplicated are stored in a common place. All scene specific prefabs are stored in the appropriate folder as stated in 1.
3) I have a scene dumper script (Editor Plugin, easy to make) which iterates every object in the current scene and generates a GameObject to Instanciate every prefab in the scene. It saves the full path to the prefab, the position, scale and rotation. This is stored in a file under /Assets/Classes/Generated/ and there is one for each scene. For example if the scene is named 0_0 then it generates /Assets/Classes/Generated/SceneLoader0_0.cs. This file can and will be added as a component when the scene loads.
4) I have an engine object which loads and unloads scenes on the fly. It typically works like this.
Get call to load scene.
Pauses all objects.
Fades screen to black
Unloads(Destroy) all GameObjects. (player,engine,camera are maintained)
Load all prefabs from the Scenes specific folder. Resources.LoadAll("Scenes/"+scene, typeof(GameObject));
Every scene folder contains a prefab which will load that script we generate from the Scene Dumper: It will call somethjing like this. gameObject.AddComponent("SceneLoader_"+scene); This script loads all common prefabs in one shot.
Unfade and Unpause the game.
This seems like a lot of work to implement and it has been. But now that the system is in place, creating my levels is very easy. With this solution, I am able to seamlessly load scenes while maintaining character input. If you are interested in going this route, reply with any questions and ill help you out.
Your answer
Follow this Question
Related Questions
Scene inside a Scene 3 Answers
issue with SceneManager.LoadScene 0 Answers
Input refresh between scenes 0 Answers
Is it better to separate Logo and Loading screen into two scenes? or just put them both in one? 1 Answer
UI button loading wrong scene 1 Answer