My character isn't showing and when i print position it says nan

Godot Version 4.2.2
It was working fine, i didn’t change anything and all of a sudden it doesn’t work

My code(i was about to add attacking but i had to go so when i got back i ran the code):

extends CharacterBody2D 

const speed = 550
var acc = 50
var friction = 70
var skill = 0
var gravity = 120
var xp = 0
var max_xp = 500
const max_jumps = 1
var current_jumps = 0
@export var jump_height : float 
@export var jump_time_to_peak : float
@export var jump_time_to_descent : float
# float variables are variables that can interact with othe objects
@onready var jump_velocity : float = ((2 * jump_height) / jump_time_to_peak) * -1
@onready var jump_gravity : float = ((-2 *jump_height) / (jump_time_to_peak * jump_time_to_peak)) * -1
@onready var fall_gravity : float = ((-2 *jump_height) / (jump_time_to_descent * jump_time_to_descent)) * -1
@onready var jump_buffer_timer = $jump_buffer_timer
var double_jump = 0
var buffered_jump = false
var facing_right = true
var wall_jump = 0
var is_jumping = false
var is_attacking = 0
var att_dmg = 50
var player_health = 100
var wall_jump_pushback = 200
var last_jump_dir = 0

func _physics_process(delta):
	
	var input_dir: Vector2 = input()
	velocity.y += get_gravity() * delta
	
	if input_dir != Vector2.ZERO:
		accelarate(input_dir)
		#play_animation() (moving animation)
	else:
		add_friction()
		#play_animation (idle)
	if Input.is_action_just_released("jump") and velocity.y < 0 :
		velocity.y = velocity.y * 0.2
	
	if Input.is_action_just_pressed("jump"):
		buffered_jump = true
		jump_buffer_timer.start()
	
	
	
	if Input.is_action_pressed("left"):
		last_jump_dir = 1
	
	if Input.is_action_pressed("right"):
		last_jump_dir = -1

	if Input.is_action_just_pressed("jump") or buffered_jump == true:
		jump()
	
	if velocity.y < 0: #if the player is going up it is jumping 
		is_jumping = true
	
	if is_on_floor() or is_on_wall(): #if it's on a wall or the floor it is not jumping
		is_jumping = false
	
	if is_on_floor():
		current_jumps = 0
	
	level_up()
	
	move_and_slide()

func input() -> Vector2:
	var input_dir = Vector2.ZERO
	
	input_dir.x = Input.get_axis("left", "right") # 1 if right -1 if left
	input_dir = input_dir.normalized()
	return input_dir

func accelarate(direction):
	velocity = velocity.move_toward(speed * direction, acc)

func add_friction(): 
	velocity = velocity.move_toward(Vector2.ZERO, friction)



func get_gravity() -> float:
	if velocity.y < 0:
		return jump_gravity
	else:
		return fall_gravity

func jump():
	if double_jump == 0 and wall_jump == 0:
		if is_on_floor():
			velocity.y = jump_velocity
			current_jumps += 1
	if double_jump == 1:
		if !is_on_wall():
			if current_jumps < max_jumps:
				velocity.y = jump_velocity
				current_jumps = current_jumps + 1
				buffered_jump = false
	if wall_jump == 1:
		if current_jumps >= 1:
			if is_on_wall() and !is_on_floor():
				if Input.is_action_pressed("right"):
					velocity.y = jump_velocity
					velocity.x = -wall_jump_pushback
					current_jumps += 1
				if Input.is_action_pressed("left"):
					velocity.y = jump_velocity
					velocity.x = wall_jump_pushback
					current_jumps += 1
				if !Input.is_action_pressed("right") and !Input.is_action_pressed("left"):
					velocity.y = jump_velocity
					velocity.x = wall_jump_pushback * last_jump_dir #if the last jump was on a left wall it pushes it towards the left if right i goes right
					current_jumps += 1
		if current_jumps == 0:
			velocity.y = jump_velocity
			current_jumps += 1

func level_up():
	if xp >= max_xp:
		max_xp = max_xp  * 1.2
		xp = 0
		skill = skill + 1

func _on_jump_buffer_timer_timeout():
	buffered_jump = false

Where’s the error?

NaN = “not a number” and you usually get that when you divide a floating point number by zero.

Look it up in the docs.

1 Like

I found the error, for some reason the variables jump_height, jump_time_to_peak and jump_time_to_descent reset themselves to 0, thanks for the help

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