From 55fa5111c25967daecf8cbf7b9bd98d1aad90cec Mon Sep 17 00:00:00 2001 From: Vivianne Langdon Date: Fri, 6 Oct 2023 02:44:51 -0700 Subject: [PATCH] improved character movement --- hero.gd | 22 ++++++++++++---------- main.tscn | 8 +++++--- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/hero.gd b/hero.gd index 7510cd8..511f4a7 100644 --- a/hero.gd +++ b/hero.gd @@ -1,13 +1,15 @@ -extends RigidBody2D +extends CharacterBody2D -@export var speed = 200.0 +@export var speed = 100.0 +@export var accel = 1000.0 func _physics_process(delta): - if Input.is_action_pressed("up"): - apply_force(Vector2.UP * speed * delta) - if Input.is_action_pressed("down"): - apply_force(Vector2.DOWN * speed * delta) - if Input.is_action_pressed("left"): - apply_force(Vector2.LEFT * speed * delta) - if Input.is_action_pressed("right"): - apply_force(Vector2.RIGHT * speed * delta) + var movement = Vector2(Input.get_axis("left", "right"), Input.get_axis("up", "down")) * speed + + if abs(movement.x) > 0.1 and sign(movement.x) != sign(velocity.x): + velocity.x = 0 + if abs(movement.y) > 0.1 and sign(movement.y) != sign(velocity.y): + velocity.y = 0 + + velocity = velocity.move_toward(movement, accel * delta) + move_and_slide() diff --git a/main.tscn b/main.tscn index b02bf3c..80c9868 100644 --- a/main.tscn +++ b/main.tscn @@ -22,11 +22,11 @@ position = Vector2(788, 69) rotation = 1.56094 blackboard = NodePath("../Blackboard") -[node name="Hero" type="RigidBody2D" parent="." groups=["Hero"]] +[node name="Hero" type="CharacterBody2D" parent="." groups=["Hero"]] position = Vector2(545, 223) -mass = 0.1 +motion_mode = 1 script = ExtResource("3_fmhqm") -speed = 2000.0 +speed = 300.0 [node name="Body" type="CollisionShape2D" parent="Hero"] shape = SubResource("CircleShape2D_eiiek") @@ -35,3 +35,5 @@ shape = SubResource("CircleShape2D_eiiek") texture_filter = 1 scale = Vector2(1.21875, 1.21875) texture = ExtResource("4_svfrs") + +[node name="AnimatableBody2D" type="AnimatableBody2D" parent="Hero"]