👟Godot Essentials Motion Component

Is a versatile and customizable module designed to streamline 2D Nodes movement in Godot Engine.

Whether top-down, platformer or grid-based, this component offers the functionality you need without having to rewrite it yourself for each project. The movement can be made flexible with the exposed parameters and contains an internal API that facilitates the most common actions that can occur in a 2D game.

Top-Down Movement Component

Platform Movement Component

Grid Movement Component

Getting Started

Exported parameters

These parameters can be modified to achieve different behaviors. Below, we explain the purpose of each parameter and its impact on the component

Speed

  • Max Speed

  • Acceleration

  • Friction

  • Air friction horizontal factor

  • Air friction vertical factor

The max speed as the name say, defines the maximum reachable limit of speed that will be apply to the velocity.

The acceleration makes smoother to reach the maximum speed if you want to increase the juiciness of the movement. In case you don't want it just assign a zero value to it and the node will reach the maximum speed immediately

The friction brakes the node smoothly to give a sliding effect when changing directions or terminating motion. In case you don't want it just assign a zero value to it and the node will not decelerate using friction

The air friction factor simulates friction for the character when in the air. This friction needs to be manually applied since it's not directly used within the component. It provides additional functionality to simulate air friction when necessary. The values of this factor apply horizontally to velocity.x and vertically to velocity.y

Modifiers

This exported group is saved for the various modifiers that may be temporarily applied to the existing parameters

  • Default temporary speed time

The Default temporary speed time In seconds, the amount of time a speed modification will endure. This value is being used on the function change_speed_temporary()

Dash

  • Dash speed multiplier

  • Times can dash

  • Dash Cooldown

  • Dash duration

  • Can dash while dashing

The dash speed multiplier is applied on maximum speed value to achieve the dash effect and move immediately the node.

The times can dash defines the number of times a dash can be performed in a row after finishing the previous one.

The dash cooldown is the recovery time to use the dash again, this applies individually even if several can be performed. Each of the dashes has its own cooldown.

The dash duration turn off the gravity the amount in seconds defined to avoid unwanted diagonal directions to the ground when is dashing.

The can dash while dashing parameter determines whether the character can initiate another dash while already in the dashing state. It is enabled by default. When this parameter is set to false, the character must wait for the first dash to finish before performing a second one.

Optional Signals

This group collect the signals that can affect the performance because of the number of times they can be executed in context. From here, you can enable or disable them:

  • Max speed reached signal

  • Stopped signal

  • Knockback received signal (true by default)

Accessible normal variables

  • body: CharacterBody2D

  • original_max_speed: float (This value is used to return to MAX_SPEED after a modifier has been applied)

  • velocity: Vector2

  • facing_direction: Vector2

  • last_faced_direction: Vector2

  • temporary_speed_timer: Timer

  • dash_duration_timer: Timer

  • dash_queue: Array[Vector2]

  • is_dashing: bool

Upon entered the scene tree:

When this node enter the scene tree happens multiple things:

  • Creates the coyote timer that it will be used when coyote jump is enabled

  • Creates the dash duration timer that disables gravity the time amount from the value dash_gravity_time_disabled

Chaining methods

Almost every method makes it return to itself (except those functions that return other values) so you can accomplish thinks like the following:

velocity_component_2d.apply_gravity()\
     .wall_climb(input_direction)\
    .wall_slide()\
    .move()

Interactive functions

move()

The basic move_and_slide from godot engine, applies the velocity to the CharacterBody2D.

move_and_collide(delta: float = get_physics_process_delta_time()) -> KinematicCollision2D

Same as above but using move_and_collide, the motion vector used internally is the body.velocity * delta

accelerate(direction: Vector2, delta: float = get_physics_process_delta_time())

When the ACCELERATION value is greater than zero, acceleration is applied to the movement. When this value is zero, applies the speed as constant.

If enabled, the max_speed_reached signal is emitted when the velocity reaches the MAX_SPEED value.

decelerate(delta: float = get_physics_process_delta_time(), force_stop: bool = false)

The velocity is decelerated until it reaches Vector2.ZERO when the FRICTION value is provided. In the opposite case, the velocity is set to Vector2.ZERO instantaneously. This deceleration can be forced using the force_stop parameter.

When the velocity reaches Vector2.ZERO, and the stopped signal is enabled, it is emitted.

accelerate_to_position(position: Vector2)

Accelerate to position defined as parameter, uses accelerate() function internally.

apply_air_friction_horizontal(friction_factor: float = AIR_FRICTION_HORIZONTAL_FACTOR)

Apply horizontal air friction to the velocity.x when this value is greater than zero. The character does not need to return is_on_floor() and is_on_wall() to true.

apply_air_friction_vertical(friction_factor: float = AIR_FRICTION_HORIZONTAL_FACTOR)

Apply horizontal air friction to the velocity.y when this value is greater than zero. The character does not need to return is_on_floor() and is_on_wall() to true.

knockback(direction: Vector2, power: float)

Executes a knockback on the character with the provided direction and force parameters for this function. A knockback_received signal is emitted

teleport_to_position(position: Vector2, valid_position_callback: Callable = _default_valid_position_callback)

This function immediately relocates the character to the specified position and emits a "teleported" signal. You can provide a callback as the second parameter to determine the validity of the target position. This callback receives both the CharacterBody2D and the target position of the function as parameters.

The _default_valid_position_callback always return true when no callable has been provided.

change_speed_temporary(new_speed: float, time: float = DEFAULT_TEMPORARY_SPEED_TIME)

Change the maximum speed of the character over the specified duration in seconds, setting the new speed as the current speed. temporary_speed_started signal is emitted.

When the timer reaches the timeout, a temporary_speed_finished signal is emitted, and the maximum speed returns to the original value before the function call.

has_available_dashes() -> bool

Returns a boolean value confirming whether dashes are available

can_dash(direction: Vector2 = Vector2.ZERO)

Returns a boolean value confirming if the character can dahs to the direction provided as parameter.

dash(target_direction: Vector2 = facing_direction, speed_multiplier: float = DASH_SPEED_MULTIPLIER)

It internally checks whether it can execute the dash, and if it can, a speed boost is applied. The dashed signal is emitted when the dash could be executed.

reset_dash_queue()

Force a clear on the dash_queue variable. This function is used internally but can also be applied manually in any case where you want to make dashes available again.

Signals

You can listen a variety of signals from this component that allow you to react and perform other actions such as displaying animations or collecting statistics.

  • max_speed_reached

  • stopped

  • knockback_received(direction: Vector2, power: float)

  • temporary_speed_started(previous_speed: float, current_speed: float)

  • temporary_speed_finished signal teleported(from: Vector2, to: Vector2)

  • dashed(position: Vector2)

  • finished_dash(initial_position: Vector2, final_position: Vector2)

  • dash_free_from_cooldown(dash_position: Vector2, current_dash_queue: Array[Vector2])

  • dash_restarted

Last updated