Trigger an action only once on button press

Godot Version

v4.2.2.stable.mono.official [15073afe3]

Question

Hi,
I’m trying to figure out how to trigger an action from an input only once when pressing a (keyboard-) key.

The problem arises from me making my camera follow the player and then wanting to toggle that on and off. It all works until i press the button just a couple miliseconds too long and it retriggers the toggle function over and over again.

Here is my code for that for refference:
public override void _Process(double delta)
{
//…

	// Toggles the locked camera input
	if (Input.IsActionPressed("camera_lock_toggle"))
	{
		c_locked = !c_locked;
	}
	if (c_locked)
	{
		Position = GetNode<CharacterBody3D>("../Player").Position;
	}
}

Thanks for the help

Couple of things I do, is to check for the action on key Released rather than pressed, or grab the time using Time.get.msec then only allow the action to happen again after so Many milliseconds has passed…

For GDScript, you could try using:

Input.is_action_just_pressed

or C#

Input.IsActionJustPressed

2 Likes

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