Godot Version
v4.6.1
Question
I am a noob and I just followed a yt tutorial and changed a couple things. the problem is that the chracaterbody just falls down and ignores the collision of the area2d. Currently using v4.6.1
extends CharacterBody2D
class_name Player
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
func respwan():
self.global_position == Vector2(1,0)
func _physics_process(delta: float) -> void:
if not is_on_floor():
velocity += get_gravity() * delta
if Input.is_action_pressed("ui_right"):
$AnimatedSprite2D.play("walk")
if Input.is_action_pressed("ui_left"):
$AnimatedSprite2D.play("walkleft")
# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction := Input.get_axis("ui_left", "ui_right")
if direction:
velocity.x = direction * SPEED
if Input.is_action_pressed("ui_right"):
$AnimatedSprite2D.play("walk")
if Input.is_action_pressed("ui_left"):
$AnimatedSprite2D.play("walkleft")
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
$AnimatedSprite2D.play("idle")
move_and_slide()
extends Area2D
func _on_body_entered(body) → void:
if body.name == “Player”:
body.respawn()
monitoring = true