- Home /
Is it possible to work around Input.getAxis always returning 0 after scene loads?
I have a player that moves based on Input.getAxis(). When the player reaches the right edge of a scene, another scene is loaded, and the same player object (using DontDestroyOnLoad) is placed at the left edge of the next scene.
Ideally, if holding the move right button through the scene load, the player would continue moving to the right since the button was never released. While it occasionally does work, frequently Input.getAxis() will simply return 0 for the axis of the key being held after the scene load, and will continue to do so until the key is released and pressed again.
Searching for this problem yields occasional over the years since 2011, none with definitive answers, but it's been a while so I figured I'd ask again. Is there a workaround?
Answer by Somian · Jun 13, 2015 at 11:59 AM
How about a GameObject that gets the axis input and doesn't get destroyed when the new scene is loaded, using DontDestroyOnLoad? http://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
EDIT: doesn't work, but:
Application.LoadLevelAdditive seems to work.
basically, your character/car/plane/whatever that takes the input is the first "level" and the other parts of the game world you load are loaded through Application.LoadLevelAdditive. Then the character with input should stay intact.
Sadly, that would not work around the problem; see chris_taylor's comment on $$anonymous$$ikael's answer. As soon as the new scene is loaded, Input.GetAxis would still return 0 despite the user not releasing the key yet. Ignoring the 0 would mean the player would keep walking even after they release the key (until they pressed and released again), honoring it results in the current behavior.
And if You use Application.LoadLevelAdditive to load the new scene? :D
(kind of the inverse solution). haven't checked with getAxis yet, but I remember Application.LoadLevelAdditive being relatively smooth.
From Ash Blue's link in the comments of chris_taylor's answer: http://forum.unity3d.com/threads/application-loadlevel-resets-input-getaxis-work-arounds.118511/#post-1177158
:(
Oh wow, I should have read later posts in that thread. Thanks! If you make it an answer I'll accept it.
Answer by chris_taylor · Jul 24, 2013 at 09:12 AM
The only thing I could think of is to write your own system with Input.GetKey. You could lerp a value to 1 and -1 based off witch key is pressed and if none is pressed lerp to 0, or any other interpolation method you want
This isn't as difficult as it sounds (don't need lerp -- can use it if you enjoy it.) And once you've done it, you start seeing new features you can add.
lerp was just an example, you can just do if less than 1 add .1 or something also
After some testing, it appears that Input.Get$$anonymous$$ey suffers the same problem. So promising, too!
Let me do some testing ill be back hopefully with an solution
Word is that pro's async does not fix this issue (http://forum.unity3d.com/threads/application-loadlevel-resets-input-getaxis-work-arounds.118511/) and custom input does not seem to fix the issue either. As pressed inputs becomes non-readable after the level transition. I'm literally dying for a solution to this, someone please save me.
Answer by Mikael-Gyth · Jul 24, 2013 at 09:29 AM
you could use localstorage to save state between sceneloads. something like(Pseudocode):
onExit
if isMoving
savedstate = isMoving
onLoad
isMoving = savedstate
Not sure if it's the best way to do it, but it should atleast yield consistent results.
Not sure if this would work. If the way he is getting input is reset to 0 and if you set a bool, is$$anonymous$$oving im gussing is a bool, then after you set is$$anonymous$$oving = savedstate it would just get set back to false unless you release the key and press again
Answer by lehart · Dec 16, 2016 at 11:56 AM
I know this is an old topic but i'm having similar issues. My application loads from a 3D room into a scene setup with a Canvas, a button and a sprite - Upon the scene load nothing works, the only way to get input is if i press ESC. Once the whole app is built even pressing esc doesn't resolve the issue. My sprite is a cursor with a follow mouse input script attached, the script makes the sprite follow the mouse cursor, the default unity cursor is hidden.
Everything works fine bar this bug on sceneload, weird thing is it doesn't do it when the user clicks a button, when that scene loads up (which is a duplicate of this scene which slight button changes) it runs fine, its only transitioning from a 3D level to a level that has no 3D objects just a canvas.
Your answer
Follow this Question
Related Questions
How do I get a value from Input.GetAxis() ? 1 Answer
Input Dead Not Being Applied to Joystick? 0 Answers
Joystick Axis 9&10 with more than 2 gamepads 0 Answers
Quirk in Input.GetAxis 0 Answers
Dualshock Dpad "sensitivity" 0 Answers