TurboTut

Untitled

Sep 29th, 2024
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends CharacterBody2D
  2.  
  3. @export var speed = 200.0  # Movement speed of the player
  4.  
  5. func _process(delta: float) -> void:
  6.     var direction = Vector2.ZERO  # Initialize the movement direction
  7.  
  8.     # Handle keyboard input for movement
  9.     if Input.is_action_pressed("ui_right"):
  10.         direction.x += 1
  11.     if Input.is_action_pressed("ui_left"):
  12.         direction.x -= 1
  13.     if Input.is_action_pressed("ui_down"):
  14.         direction.y += 1
  15.     if Input.is_action_pressed("ui_up"):
  16.         direction.y -= 1
  17.  
  18.     # Normalize direction and multiply by speed for consistent movement
  19.     velocity = direction.normalized() * speed
  20.  
  21.     # Move the player using the calculated velocity
  22.     move_and_slide()
  23.  
Advertisement
Add Comment
Please, Sign In to add comment