- Home /
The question is answered, right answer was accepted
Inputs not working after build
I am working on a platformer in unity, and just to check if everything was working, I built it as the PC, Mac & Linux Standalone, and I encountered a strange issue: The game would detect if a key was being pressed, or had just started being pressed, but not when it had been released meaning that when I pressed a key to move left or right, I would continually move in that direction until another key is pressed, regardless of whether I am still holding down the initial key. A similar issue happens with jumping, the game should have a double jump, but I can single jump, because the game still thinks I am holding down jump button from the first jump.
Here are some code samples if anyone is interested (everything works fine inside the editor and uses the new input system):
//OnRight or OnLeft is triggered when a key is pressed or released, so I made a bool to detect whether it was being held down
//press d to move right
public void OnRight()
{
if (isRight == false)
{
isRight = true;
}
else
{
isRight = false;
}
}
//press a to move left
public void OnLeft()
{
if (isLeft == false)
{
isLeft = true;
}
else
{
isLeft = false;
}
}
And this one:
//this allows the key presses to move you
if (isLeft == true)
{
body.velocity = new Vector2(speed * -1, body.velocity.y);
}
if (isRight == true)
{
body.velocity = body.velocity = new Vector2(speed, body.velocity.y);
}
Why not just use Input.GetKeyDown
? What if you do this instead :
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
body.velocity = new Vector2(speed * -1, body.velocity.y);
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
body.velocity = body.velocity = new Vector2(speed, body.velocity.y);
}
Please show how you are using Input.GetKeyDown()
and Input.GetKeyUp()
.
I am not using GetKeyDown or GetKeyUp, because I am using the new input system's sendMessage, I should have probably made that clearer in my post.
Answer by CodeMonkeyYT · Apr 25 at 06:58 AM
Are you using the new Input System? If so you need to add the Press interaction set up with Press and Release https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Interactions.html#press
I covered the new Input System in detail in this tutorial https://unitycodemonkey.com/video.php?v=Yjee_e4fICc
Although that should work the same way in the Editor or in a Build, maybe you have some error happening in your Build, check the log files https://unitycodemonkey.com/video.php?v=2ta9-N3kvYs
I am using the new Input system, I did these things and, in the logs, I couldn't find anything unusual, but when adding the press interaction with press and release, in the editor, it now acted as it did in build, except I could stop the movement by pressing the key for a second time, in build, the result was the same as it was previously. The reason I came across the method I used initially was that sendMessage was not sending messages when the key was being held, only when it was pressed or released, and so I created a bool variable that flips every time the message is received, so I know when the key is being held down, it works fine in the editor, but not in build.
Those inputs have multiple events, Started, Performed and Cancelled, it doesn't seem like you have any logic checking for that, so it's possible that when you're pressing a button you're flipping the bool 3 times on all 3 of those events.
I would encourage you to use the C# method so you can check for the event state and use that to set the bool instead of making your logic based on expecting a certain number of function calls.
Thanks for the help, it took a while to get working but now I have done it. The code is neater now the new input system is working but it turns out the issue I was adding in the new input system to fix turned out to be an issue with the friction of the objects, and me putting a timer in update instead of fixed update. Again, thanks for the help.
Answer by barry1251 · Apr 30 at 10:19 AM
I have tried setting InputSystemUIInputModule's pointer behaviour to all available options, and I've tried setting the Project Settings > Input System Package > Update Mode to all available options also.
Follow this Question
Related Questions
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
Unity UI: Enable / Disable specific Joysticks for Interaction 0 Answers
Add Mobile Android and iOS Emojis 1 Answer
Detect when no input is recieved 2 Answers
Local Multiplayer Game | Input for player 2 completely broken. 0 Answers