Godot Version
4.2.1
Question
I am trying to put together character movement code for a game i am working on however i cant figure out why my code doesnt work. i have experience in python and c# but GDscript is new to me. below is the code ive created to try get it to work but have had little success
extends CharacterBody2D
# Constants
const GRAVITY = 800
const MOVE_SPEED = 200
const JUMP_FORCE = -500
const DASH_SPEED = 800
const DASH_DURATION = 0.2
const CLIMB_SPEED = 150
# State variables
var current_velocity : Vector2 = Vector2()
var is_on_ground : bool = false
var is_climbing : bool = false
var dash_timer : float = 0.0
func _process(delta: float) -> void:
# Handle movement and input
handle_input(delta)
# Apply gravity
current_velocity.y += GRAVITY * delta
# Move the character
move_and_slide()
# Update dash timer
dash_timer = max(0, dash_timer - delta)
# Handle player input
func handle_input(delta: float) -> void:
current_velocity.x = 0
if is_on_ground:
# Ground movement
current_velocity.x = MOVE_SPEED * Input.get_action_strength("ui_right") - MOVE_SPEED * Input.get_action_strength("ui_left")
# Jumping
if Input.is_action_just_pressed("ui_up"):
current_velocity.y = JUMP_FORCE
is_on_ground = false
# Climb
if is_climbing:
current_velocity.y = CLIMB_SPEED * Input.get_action_strength("ui_down") - CLIMB_SPEED * Input.get_action_strength("ui_up")
# Dash
if Input.is_action_just_pressed("dash") and dash_timer == 0:
dash_timer = DASH_DURATION
current_velocity.x = DASH_SPEED * sign(current_velocity.x)
# Handle collisions
func _on_Floor_body_entered(body: PhysicsBody2D) -> void:
is_on_ground = true
current_velocity.y = 0
func _on_Floor_body_exited(body: PhysicsBody2D) -> void:
is_on_ground = false
func _on_Climbable_body_entered(body: PhysicsBody2D) -> void:
is_climbing = true
func _on_Climbable_body_exited(body: PhysicsBody2D) -> void:
is_climbing = false
# Helper function to get the sign of a number
func sign(x: float) -> float:
if x > 0:
return 1
elif x < 0:
return -1
else:
return 0->