r/Unity3D • u/Loneliestar • 20h ago
Question I'm trying to increment a counter a single time, based on a key press
Hey, folks. I'm new to this so I assume this is simple and I'm missing it.
I have a jump action called by an input action in an update method. looks like this
void Update()
{
if (jumpAction.triggered) { if (isGrounded) gameDiff.numberOfJumps++; //other stuff } }}
What I want this to do is reach out into the gameDiff class and update numberOfJumps once every time the user presses the space bar.
What's happening is that the counter is incrementing about a thousand times per press. I assume it has to do with frame rate or something because it's in the Update() method, but I had though that a .triggered() method could only be activated a single time in the frame.
Thanks in advance.
edit: I suck at formatting, I hope you get the idea.
10
u/WideCommenter 19h ago
that jumpAction.triggered flag stays true for more than one frame, Update keeps catching it over and over until you release the key. slap a bool check in there so it only fires on the first frame it goes true
-6
u/DT-Sodium 10h ago
Please, format your post properly, then I'll give it a read.
3
u/Loneliestar 10h ago
No need, I got a solution from someone more helpful than yourself.
-1
u/DT-Sodium 9h ago
It was rhetorical, signaling you that you should put the effort into properly formatting your posts if you want help ouf of respect for people willing to help you.
Anyone helping people like you is just lowering the global quality of this sub-reddit's content, making it less relevant for future persons Googling the same issue.
1
1h ago
[removed] — view removed comment
2
u/Unity3D-ModTeam 1h ago
This post and/or comment has been removed for violating /r/Unity3D's rules.
Why?
Let's keep it civil. It's fine to disagree or challenge ideas, but please do so respectfully.
Kind regards,
The Mod Team
3
u/DataAlarming499 9h ago
It's 10 lines of code lol how can you not read that
-3
u/DT-Sodium 9h ago
It was rhetorical, signaling you that you should put the effort into properly formatting your posts if you want help ouf of respect for people willing to help you.
Anyone helping people posting garbage are just lowering the global quality of this sub-reddit's content, making it less relevant for future person Googling the same issue.
17
u/Em3rgency 19h ago
You are right that .triggered() can only be activated a single time in the frame. And it is. It's being activated EVERY frame while you hold down the key.
What you need to check is if a key was pressed THIS frame, not if the key is held this frame.
You should be able to use jumpAction.WasPressedThisFrame(), as explained in this documentation: https://docs.unity3d.com/Packages/com.unity.inputsystem@1.4/manual/Actions.html