Move_and_slide and velocity

Godot Version

Godot 4

Question

I dont know why this is not working

this is the script
extends CharacterBody3D

const moveSpeed = 1
const turnSpeed = 180
const gravity = 98
const maxFallSpeed = 30
@onready var anim = $AnimationPlayer

var yVelo = 0
var grounded = false

func _physics_process(delta):
var moveDir = 0
var turnDir = 0
velocity = Vector3.ZERO
if Input.is_action_pressed(“Forwards”):
moveDir += 1
if Input.is_action_pressed(“Backwards”):
moveDir -= 1
if Input.is_action_pressed(“Leftwards”):
turnDir += 1
if Input.is_action_pressed(“Rightwards”):
turnDir -= 1
rotation_degrees.y += turnDir * turnSpeed * delta
velocity.x = moveSpeed * moveDir
velocity.z = moveSpeed * moveDir
velocity.y = yVelo
move_and_slide()
the character is only moveing on 1 axis which is diagonal and doesn’t change when i turn the character

You are adding or subtracting 1 from the vector, resulting it to be in only four states.
Personally, I use this code for character moving:

var input_dir = Input.get_vector("left", "right", "forward", "backward")
var direction = (head.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	
if not is_on_floor():
	velocity.y -= 4 * gravity * delta
if is_on_floor():
	if Input.is_action_just_pressed("jump"):
		velocity.y = jump
	if Input.is_action_pressed("run"):
		speed = run
	else:
		speed = walk
	if direction:
		velocity.x = direction.x * speed
		velocity.z = direction.z * speed
	else:
		velocity.x = lerp(velocity.x, direction.x * speed, delta * 4)
		velocity.z = lerp(velocity.z, direction.z * speed, delta * 4)
else:
	velocity.x = lerp(velocity.x, direction.x * speed, delta)
	velocity.z = lerp(velocity.z, direction.z * speed, delta)
move_and_slide()

my character doesn’t have a head the cameras are in the world space not on the character

Oh I see. Then I think you should try changing the rotating speed as it will rotate the object by 180 degrees in case of ~60 fps according to calculations. Try setting it to like 15 or 30