Move_and_slide() provides no movement

Godot Version

Godot 4.2.1

Question

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
	#print(vel)
	
	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
	#print(velocity)
	
	move_and_slide()

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.

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.