Godot 4.4.1
I have started to switch my game to component-based, and the one script that I have so far is for player movement. And after doing all that I can, the player won’t move. What can I do to fix this? I can send my Github repo if needed. I am also not able to post a video since I am a new user.
Check your code from start to finish, how does _physics_process start, does it end up in your movement component? Maybe trying using the print
function at each step or using breakpoints to keep track of execution. Can’t give specific advice without specific code, if you’d like to paste a sample you can do so directly on the forum
Posting here so to be linked for later use instead of re-typing this every time
Make sure you paste scripts instead of screenshots. To properly format pasted code use three ticks ``` at the start and end of your paste like so:
```
# type or paste code here
func _ready() -> void:
print("My ready function!")
```
Results in:
# type or paste code here
func _ready() -> void:
print("My ready function!")
Press the </> button or ctrl+e in the forum to create these ticks.
[2024-09-02…
Here is the code I hope it helps.
class_name PlayerMovementComponent
extends CharacterBody2D
# Nodes in Player Scene
@export var animated_sprite: AnimatedSprite2D
@export var player: CharacterBody2D
# Walk Variables
@export var walk_speed: float = 200.0
@export_range(0, 1) var acceleration: float = 0.1
@export_range(0, 1) var deceleration: float = 0.1
# Jump Variables
@export var jump_force: float = -400.0
@export_range(0, 1) var decelerate_on_jump_release: float = 0.5
var jump_max: int = 2
var jump_count: int = 0
# Dash Variables
@export var dash_speed: float = 700.0
@export var dash_max_distance: float = 300.0
@export var dash_curve: Curve
@export var dash_cooldown: float = 1.0
var is_dashing: bool = false
var dash_start_position: float = 0
var dash_direction: float = 0
var dash_timer: float = 0
@onready var gravity: float = ProjectSettings.get_setting("physics/2d/default_gravity")
func gravity_init(delta: float) -> void:
if not is_on_floor():
player.velocity.y += gravity * delta
func walk() -> void:
var direction: float = Input.get_axis("Walk_Left", "Walk_Right")
if direction:
player.velocity.x = move_toward(player.velocity.x, direction * walk_speed, walk_speed * acceleration)
animated_sprite.flip_h = direction < 0
if is_on_floor():
animated_sprite.play("Walk")
else:
player.velocity.x = move_toward(player.velocity.x, 0, walk_speed * deceleration)
if is_on_floor():
animated_sprite.play("Idle")
func jump() -> void:
# Reset jump count when touching the floor or wall
if (is_on_floor() or is_on_wall()) and jump_count != 0:
jump_count = 0
# Jumping logic
if Input.is_action_just_pressed("Jump") and jump_count < jump_max:
player.velocity.y = jump_force
jump_count += 1
animated_sprite.play("Jump")
# Modify jump height if the button is released early
if Input.is_action_just_released("Jump") and player.velocity.y < 0:
player.velocity.y *= decelerate_on_jump_release
func dash(delta: float) -> void:
# Dash Activation
var direction: float = Input.get_axis("Walk_Left", "Walk_Right")
if Input.is_action_just_pressed("Dash") and direction and not is_dashing and dash_timer <= 0:
is_dashing = true
dash_start_position = position.x
dash_direction = direction
dash_timer = dash_cooldown
# Performing Dash
if is_dashing:
var current_distance: float = abs(position.x - dash_start_position)
if current_distance >= dash_max_distance or is_on_wall():
is_dashing = false
else:
player.velocity.x = dash_direction * dash_speed * dash_curve.sample(current_distance / dash_max_distance)
player.velocity.y = 0
animated_sprite.play("Dash")
# Reduce dash cooldown
if dash_timer > 0:
dash_timer -= delta
Looks fine, how do you call walk()
? Can you show that script now?
I found out a way to make it work. Thank you for your help!!