Turning audio source on on GetKey vs on GetKeyDown

Rick’s code turns on Audio source on GetKey and turns it off when the key is no longer pressed. I implemented it as turn on on GetKeyDown and turn off on GetKeyUp. With GetKeyDown/GetKeyUp combo, I don’t need to check if the audio is already playing. It seems to create the same outcome. Is one implementation better than the other?

Hi @book-3,

Welcome to our community! :slight_smile:

Your solution is perfectly fine. To be honest, I don’t know which approach is better. The idea behind using GetKey is to ensure that user input does not get missed during one frame, but I never encountered a case where a released key was missed by Unity.

Pressing a key down could be missed due to key ghosting, which is an unsolveable hardware problem. (Unsolveable for us as game developers because we cannot control what hardware our future players use.)

Feel free to use your solution. If an issue occurs due to it, regard it as a valuable opportunity to learn something about user input. Making “mistakes” is the best way to learn more about a system because the interesting details often cannot be found in a manual.

I hope this helped a bit. :slight_smile:


See also:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms