Advertisement
humpda

Character_2D_keyboard_move

May 7th, 2025 (edited)
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends CharacterBody2D
  2. var speed: float = 100 # Speed of the player in pixels per second
  3. func _physics_process(delta):
  4.  # Reset velocity at the start of every frame
  5.     velocity.x = 0
  6.     velocity.y = 0
  7.      # Handle arrow key input
  8.     if Input.is_key_pressed(KEY_RIGHT):
  9.         velocity.x += speed
  10.     if Input.is_key_pressed(KEY_LEFT):
  11.         velocity.x -= speed
  12.     if Input.is_key_pressed(KEY_UP):
  13.         velocity.y -= speed
  14.     if Input.is_key_pressed(KEY_DOWN):
  15.         velocity.y += speed
  16.      # Apply velocity to move the player and handle collisions
  17.     move_and_slide()
  18.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement