how to make an action happen with a press of a key

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Munchobi

I am a complete beginner and I was trying to learn Godot by making pong.
I did make it but I want to add a feature.

So, I made the ball start at the center, but the problem is that the ball starts moving immediately once I run the program and I don’t want that to happen rather I want to make it so that when I press enter or something else the ball then starts moving.

I tried to achieve this using this
if Input.is_action_pressed: move()

if Input.is_action_just_pressed: move()

but what I encountered was that I had to keep pressing the button to not stop the ball from moving, the ball doesnt start moving with a single click of the button.

My full code:

if Input.is_action_pressed:
            #movine the ball
	var collision = move_and_collide(direction * delta)
	#checking for collision
	if collision:
		#bouncing the ball
		direction = direction.bounce(collision.normal)

I want something like this to happen
this is not the whole code but I assume this is enough

Adding to @Inces answer below, it’s likely that your ball is a RigidBody2D node. If it’s a normal KinematicBody2D, you probably turned on the Motion->Sync to Physics parameter. By default, physics enabled bodies will feel gravitational pull downwards, that’s why your ball is moving before you press anything.

MisterMano | 2021-10-12 14:51

I think I failed to communicate my question properly, sorry for that guys.

  1. The ball is a static body
  2. I have no issues with gravity
  3. The game works how I intend it to work
  4. The real problem is: The game runs immediately when I run the program, so I want the ball to start moving or the game to start running only after I press a key. Basically what I am trying to say is:
    I run the program → you press a key → then the ball starts moving and the game starts

Munchobi | 2021-10-12 15:40

Thank you for your time! I somehow got it working!

Munchobi | 2021-10-13 03:39

:bust_in_silhouette: Reply From: Inces

Nope, this is not the code that is required here :slight_smile:

Something makes your ball move from the start. I assume that is gravity. Did You set it in code or used default rigid body settings ? You have to disable gravity until button is pressed or player touches the ball. Take your ball and introduce state variables : something like beforegame and ingame. Next take your gravity applying code and insert condition there : apply gravity but only if state is ingame. Pressing enter will change balls state from beforegame to ingame, but only if state is beforegame. Scroing a point will set ball state to beforegame again. This is what state machines are for.

Please check the reply I gave to MisterMano, I think I failed to communicate my question to you, I am really sorry for that

Munchobi | 2021-10-12 15:41

Surprisingly, I somehow got it working! Thank you for your time!

Munchobi | 2021-10-13 03:38