Move_and_slide() provides no movement

Godot Version

Godot 4.2.1


I’m having issues with the CharacterBody2D node, specifically the move_and_slide() function. Currently, the character simply doesn’t move. I’ve started printing bunch of variables to see where something is going wrong. What I’ve checked so far is too small of a velocity (multiplied by 1000 and nothing changed), whether or not input is working (print on key press works), and whether or not velocity is changing (printing velocity variable shows the change). I’m still confused on what could be preventing the node from moving. Does anyone have any wisdom on this? I can provide project files if needed.

Here’s the script I wrote for the movement:

extends CharacterBody2D

# px / s for all speed units
@export var speedBase = 300
@export var speedCap = 300
@export var friction = .5
var vel = Vector2.ZERO

@export var dashBase = 600
@export var dashFriction = .6
@export var dashCooldown = 1
var dashVel = 0

var iFrame = false

var timer = 0

func _process(delta):
	timer += delta

func _physics_process(delta): 
	dashVel *= dashFriction
	vel += Input.get_vector("left","right","up","down") * speedBase
	if Input.is_action_pressed("dash") && timer > dashCooldown:
		dashVel += dashBase
	#if !vel.is_normalized():
		#vel = vel.normalized()
	vel *= friction
	if vel.length() > speedCap:
		vel *= (speedCap / vel.length())
	velocity = vel
	velocity.x += (vel.x/(abs(vel.x) + abs(vel.y))) * dashVel
	velocity.y += (vel.y/(abs(vel.x) + abs(vel.y))) * dashVel

idk if you realized after you print the velocity in the next line, it gives nan,nan value because you are trying to divide something with 0.0 with that abs(vel.x) + abs(vel.y)

I did see that and planned to fix it, but I didn’t think it would have an effect when the value isn’t nan (when key is pressed). I’ll try fixing that and see what happens. Thanks.

The first person to respond mentions this. It’s because when velocity = (0,0) the two lines applying the dash velocity divide by zero. Like I mentioned, I knew it was an issue but assumed it was unrelated to the lack of movement because velocity does change to (300,0) when a single key is pressed.

That was it. Figured it wouldn’t affect it because I could see velocity changing to other values but I guess not. Thanks!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.