Additional player speed

Hello everyone I would like to create the game of my dreams, and this is my first project, so I’m still new to programming.

Please help me with the code. I tried to replicate the Celeste mechanics where a player can be blown away by the wind as a separate force that acts on the player’s direction. I’m stuck, so any help would be greatly appreciated. Thank you.

extends CharacterBody2D

const JUMP_VELOCITY = -400.0

@export var SPEED = 250.0
@export var boost = 15

var jerk_speed : float

the speed of the player himself

var speed_plaer : Vector2

#additional player speed
var speed_bost : Vector2 = Vector2(20, 8)

var direction : Vector2 = Vector2.ZERO

Get the gravity from the project settings to be synced with RigidBody nodes.

var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)

func _physics_process(delta):

direction = Input.get_vector("Left", "Right", "Up", "Down")
if not is_on_floor():
	velocity.y += gravity * delta

# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
	velocity.y = JUMP_VELOCITY


velocity.x = speed_plaer.x  + speed_bost.x
if direction.x >= 0.5 :
	speed_plaer.x = clamp(min(velocity.x+ boost,  SPEED), -SPEED, SPEED)  
elif direction.x <= -0.5 :
	speed_plaer.x = clamp(max(velocity.x- boost, -SPEED), -SPEED, SPEED)
elif direction.x == 0 and is_on_floor(): 
	speed_plaer.x = move_toward(speed_plaer.x, 0, 90)
elif direction.x == 0 and not is_on_floor():
	speed_plaer.x = move_toward(speed_plaer.x, 0, 5)

move_and_slide()

What does this mean? What do you expect your code to do? What is happening instead? With the amount of information you’ve given it will be hard to help you.

In this code, an attempt is made to split the speed_player: the character’s own velocity vector, and speed_boost: an additional velocity vector applied to the player. There is also a smooth acceleration of the player. However, the speed_boost does not allow the player to accelerate themselves, and I lose control of the player’s movements as they are carried away in the direction indicated by the speed_boost. I expected the player to resist the speed_boost direction, but nothing happened

There’s simply no case that would ever result in a reduced x velocity. speed_player.x would need to get negative in order to resist the speed_bost, but it never does!

velocity.x = speed_plaer.x + speed_bost.x

speed_plaer is not initialized, so implicitly (0, 0). speed_bost is (20, 8). So in the very first frame, your x velocity will be positive 20: the character will move to the right.

As long as there’s no input in x direction, that won’t change!

If there is enough input in x direction, speed_plaer will always increase:

Case 1: “Right” pressed

speed_plaer.x = clamp(min(velocity.x+ boost,  SPEED), -SPEED, SPEED)

clamp(min(20 + 15, 250.0), -250, 250)clamp(min(35, 250.0), -250, 250)clamp(35, -250, 250) → 35

Case 2: “Left” pressed

speed_plaer.x = clamp(max(velocity.x- boost, -SPEED), -SPEED, SPEED)

clamp(max(20 - 15, -250), -250, 250)clamp(max(5, -250), -250, 250)clamp(5, -250, 250) → 5

So the next frame, velocity.x will be even bigger (either 35 + 20 or 5 + 20).

1 Like

Thank you very much