Velocity.x is strange

I followed all the steps in the video and corrected any errors along the way. However, I’m running into an issue where velocity.x = x isn’t affecting the x coordinates as it should. I’ve double-checked the numbers and confirmed that all functions are correctly passing to jump_side().

In the fragment, it’s supposed to push the player back when his collide with an enemy

20:38

Maybe the x-value is getting overwritten by some other code?

here is the console where Im checking character velocity in time:
zero is nothin
400 moving
500 is setting direction to push away from enemy


0
0
0
0
400
400
400
400
400
500
500
0
0
0
0
and so on

so its changed, but in short time, might be need to increase the setting time using timer However, this may prevent the character from moving.

Can you show your script? Make sure to paste with formatting, the </> button or ctrl+e in the forum will create three ticks for you like so

```
type or paste code here
```

There’s a function called jump_side that accepts a variable as its input., in the second part on 27 line. im calling this function
Entire setup serves as a signal for when the character collides with an enemy.

extends CharacterBody2D


const SPEED = 400.0
const JUMP_VELOCITY = -900.0
@onready var sprite_2d = $Sprite2D

func jump():
	velocity.y = JUMP_VELOCITY
	
func custom_jump(x):
	velocity.y = x

func jump_side(x):
	velocity.y = JUMP_VELOCITY
	velocity.x = x
	
func damage_anim() -> void:
	anim_separate("hit")

var prima = false
func anim_separate(name):
	match name:
		"jumping":
			if (prima == true):
				await sprite_2d.animation_finished
			sprite_2d.animation = "jumping"
			prima = false
		"idle":
			if (prima == true):
				await sprite_2d.animation_finished 
			sprite_2d.play("idle")
			prima = false
		"hit":
			prima = true
			sprite_2d.play("hit")
		"running":
			if (prima == true):
				await sprite_2d.animation_finished 
			sprite_2d.play("running")
			prima = false

func _physics_process(delta: float) -> void:
	if (velocity.x > 1 || velocity.x < -1):
		anim_separate("running")
	else:
		anim_separate("idle")

	if not is_on_floor():
		velocity += get_gravity() * delta
		anim_separate("jumping")
		
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	var direction := Input.get_axis("left_move", "right_move") 
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)

	move_and_slide()
	
	if Input.is_action_just_pressed('left_move'):
		sprite_2d.flip_h = true
	elif Input.is_action_just_pressed('right_move'):
		sprite_2d.flip_h = false
extends RigidBody2D

@onready var game_manager: Node = %"Game Manager"
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D

func _on_area_2d_body_entered(body: Node2D) -> void:
	if body.name == "CharacterBody2D":
		var y_delta = position.y - body.position.y
		var x_delta = body.position.x - position.x
		if (y_delta > 30):
			animated_sprite_2d.play("hit")
			body.custom_jump(-1400)
			
			await animated_sprite_2d.animation_finished
			queue_free()
		else:
			game_manager.decrease_health()
			if (x_delta > 30):
				body.jump_side(-500)
			else:
				body.jump_side(500)
			body.damage_anim()

Your player sets their velocity in _physics_process; if you want jump_side to affect velocity then your movement here must be calculated as an acceleration instead of abruptly setting speed. move_toward is good, but used somewhat incorrectly here, the third parameter being SPEED means it will slow down by the player’s maximum speed every frame, essentially instantly.

Try this, the player will accelerate to full speed in about a quarter second

var direction := Input.get_axis("left_move", "right_move")
var acceleration := SPEED * delta * 4
if direction:
	velocity.x = move_toward(velocity.x, direction * SPEED, acceleration)
else:
	velocity.x = move_toward(velocity.x, 0, acceleration)

In the video they had this changed to 12 instead of SPEED, it is still a frame-dependent value and doesn’t fix the issue if the player is moving while getting hit.

else:
	velocity.x = move_toward(velocity.x, 0, 12)
2 Likes