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()