Opanuj CharacterBody2D – Godot 4 Poradnik

Assety: https://www.kenney.nl/assets/pixel-shmup

# TOP-DOWN poruszanie
extends CharacterBody2D

@export var speed = 400

func get_input():
	var input_direction = Input.get_vector("left", "right", "up", "down")
	velocity = input_direction * speed

func _physics_process(delta):
	get_input()
	move_and_slide()



# Poruszanie auta
extends CharacterBody2D

@export var speed = 400
@export var rotation_speed = 1.5

var rotation_direction = 0

func get_input():
	rotation_direction = Input.get_axis("left", "right")
	velocity = transform.x * Input.get_axis("down", "up") * speed

func _physics_process(delta):
	get_input()
	rotation += rotation_direction * rotation_speed * delta
	move_and_slide()

# Poruszanie Platformówka 
extends CharacterBody2D

const GRAVITY = 300.0
const WALK_SPEED = 200
const JUMP_SPEED = -250.0

func _physics_process(delta):
	velocity.y += delta * GRAVITY
	var sprite = $AnimatedSprite2D

	if Input.is_action_pressed("ui_left"):
		velocity.x = -WALK_SPEED
		sprite.flip_h = false
		sprite.play()
	elif Input.is_action_pressed("ui_right"):
		velocity.x =  WALK_SPEED
		sprite.flip_h = true
		sprite.play()
	else:
		sprite.pause()
		velocity.x = 0
		
	if is_on_floor() and Input.is_action_just_pressed("ui_accept"):
		velocity.y = JUMP_SPEED
	move_and_slide()
	#var collision = move_and_collide(velocity * delta)
	#if collision:
		#velocity = velocity.bounce(collision.get_normal())