I want to shoot some bullets while holding down spacebar. On Windows, there is a configurable option for a “Repeat Delay” when holding down a key.
So the signal for the input goes something like this:
Input > delayyyyyyy> Input> Input> Input> Input> Input> etc
For my game I want there to be no delay, and instead work like this:
Input> Input> Input> Input> Input> Input> etc
Since this is something controlled by the operating system, I don’t suppose there is any way to overwrite this behaviour while the game is running?
I know in many games this system behaviour is ignored, and I’m guessing they use a more direct route to the hardware keyboard. But is there a way to do this in Unity?
If you use GetButtonDown or GetKeyDown, they return true as long as the key is held down, so the system delay or lack thereof isn’t relevant. Actually, I’m not sure what sort of input code you could even do in Unity where the system delay would have any effect.
Well, that is what I’m using, and I see the effect of the system’s repeat delay.
Try it:
using UnityEngine;
using System.Collections;
public class temptest : MonoBehaviour
{
private float time;
void Update()
{
time += Time.deltaTime;
if (Input.GetKey(KeyCode.Space))
{
Debug.Log(time);
}
}
}
Run and hold down space - watch the console:
0.5369185
1.033918
1.057661
1.091631
Notice the delay between the first and second, and then after the second the delay is miniscule.
No, GetKey returns true every frame as long as the key is held down. It doesn’t return true/false/true/false or anything. Or if it does, then something is quite strange on your system.
if (Input.GetKey(KeyCode.Space))
{
Debug.Log(time);
}
else
{
Debug.Log("no key");
}
As long as the space key is held, it never prints “no key” here, only the time.
In any case, I’d recommend GetKeyDown if you’re implementing some kind of auto-fire.
if (Input.GetKeyDown (KeyCode.Space)) {
InvokeRepeating ("Fire", .01, repeatRate);
}
else if (Input.GetKeyUp (KeyCode.Space)) {
CancelInvoke ("Fire");
}
Hm, it looks like I only experience this problem when I use Teamviewer on my Laptop to connect to my PC at home. Doing this directly on the PC does not have this problem. Oops. My bad!
I would guess in that case it’s actually sending key up/down events repeatedly when you hold a key down. In which case the GetKeyDown/Up code wouldn’t work either. Actually nothing would work.
I’ve also come across this problem in a Linux build of one of our games - a user has reported that while attempting to move the main character using the left/right arrow keys, the movement is slow. On further investigation, I can see that the character moves one frame to begin with, then there is a half-second delay, then movement is on/off after that. So basically identical to a key being held down when typing.
Is it the case that the operating system is sending Unity key up/down events periodically, and Unity has no way of knowing that the key is being held down? Or is there anything that can be done about this?
I’m using Unity 5.6.0 and Input.GetKey() only to detect keyboard input.
See if you can create a small reproducible test case (including the behavior with GetKeyUp and GetKeyDown, in case it behave as described above), and then report it as a bug to Unity. I mean, a big part of me wants to just blame Linux and call it a day, but Unity needs to know that this happens, and I’m pretty sure they have less testing resources for input-related stuff dedicated to Linux than other platforms (it’s largely expected that Unity for Linux is largely running game servers, I think).
Sadly I can’t, because it doesn’t happen on our test machines. Or on 99% of systems, Linux or otherwise by the sound of it. It’s literally happened for one user out of hundreds, and I only detected it because he was kind enough to send me a video where I could see this occurring.
It may not even be a Unity problem, although he did say other games worked fine.
I’m fairly new to forums/bug reporting so I’m not sure if this description alone will be enough to report a bug - obviously under normal circumstances I would happily take the time to create a project to reproduce it. I’ll read up on bug reporting and see what I can do.
Well, a bug report with anything about that user’s Linux setup that he’s willing to share (hardware, Linux distro/version, etc) would be helpful to narrowing down the problem, at least.
I agree, and I’ve reported it and attached a sample project that works on my system but won’t work on an affected one, much like guitarxe’s example above. We’ll see what happens.