Linear_velocity crashes game on start

Godot Version

Godot 4.2.2 Steam

Question

Okay, so I’m trying to port my character controller over from a unity project. The problem is that it gives a black screen and then pauses the game whenever it runs the _push_forward function.
The script in godot looks like this:

extends RigidBody3D
@onready var coll = $Collider
const walkSpeed = 4
const runSpeed = 8
const jumpForce = 30
const startForce = 200
const stopForce = 400
const airControl = 15
@onready var head = $Head
const lookSpeed = 0.25
const lookXLimit = 85
var canLook = true
var canJump = true
var canMove = true
var rotationX = 0
var look_rot : Vector2

func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func _physics_process(delta):
var input_dir = Input.get_vector(“move_left”, “move_right”, “move_forward”, “move_backward”)
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
_push_toward(direction * walkSpeed, startForce, stopForce)
head.rotation_degrees.x = look_rot.x
rotation_degrees.y = look_rot.y

func _input(event):
if event is InputEventMouseMotion:
look_rot.y -= (event.relative.x * lookSpeed)
look_rot.x -= (event.relative.y * lookSpeed)
look_rot.x = clamp(look_rot.x, -lookXLimit, lookXLimit)
if event.is_action_pressed(“exit”):
get_tree().quit()

func _push_toward(targetVelocity, maxStartingForce, maxStoppingForce):
var deltaV = targetVelocity - linear_velocity
var force = mass * deltaV / get_process_delta_time()
force.y = 0
var forceLimit = maxStoppingForce if (deltaV.dot(linear_velocity) < 0) else maxStartingForce
if force.length() > forceLimit:
force = force.normalized()
force = force * forceLimit
apply_force(force)

The line that causes the problem is the line where I create the deltav variable. If a add a line before that to print the linear_velocity property, it will return a normal number as the y value, but x and z are “nan” If I change the script to only run the _push_forward function when the player is trying to move, the game doesn’t break immediately, but does as soon as I press w. Is there a different way to get the player’s velocity? I was super happy with this script in unity as it both felt good to control and was fully physical so I could easily add new movement mechanics like rocket jumping.

Var direction is passed to _push_toward

Be careful about normalized() if vector is zero. I.e. when there is no player input.

Thanks for replying, I tried removing the normalized part to see if it would start, and it did the same thing. Then I tried to do an if statement to see if it was zero and only normalize it if it wasn’t. That didn’t work either.