Inline if statement not working

I’m not sure what else to say besides I dont slow down.

func _physics_process(delta:float) -> void:
	#Get Direction
	dir = Input.get_axis("left", "right")
	target_speed = walk_speed

func ground(delta):
	#Ground Accel and Deccel
	velocity.x = move_toward(velocity.x, dir * target_speed, (ground_acel if dir else ground_decel) * delta)

If I print dir when there is dir, it only prints when im moving, so i would expect this inline statement to work.

All the values are set correctly.

where do you call air? What are the values of air_acel and ground_decel?

Edited post so check it out again.
I do not deccel on the ground.
ground_acel is 400 and ground_decel is 60000 at the moment lol.

Ok, where do you call ground?

In the physics process when not is_on_ground()

Could you paste your real script? Your sample doesn’t call ground.

1 Like
class_name Player extends CharacterBody2D

#Refrences
@export_group("Refrences")
@export var cam:Camera2D

#Stats
@export_category("Stats")
@export_group("Speed")
@export var walk_speed:float

@export_group("Jump")
@export var jump_power:float
@export var jump_boost:float
@export var jump_cool:float
@export var jump_buff_amount:float
@export var coyote_time_amount:float

@export_group("Physics")
#Ground Physics
@export var ground_acel:float
@export var ground_decel:float
#Air Strafing
@export var air_acel:float
@export var air_decel:float
@export var gravity:float = 980

@export_group("Bob")
@export var bob_freq := 2.5
@export var bob_amp := 0.1

#Refrences
#@onready var speed_gui = globals.game.gui.get_child(0)
@onready var jump_buff:SceneTreeTimer
@onready var coyote_timer:SceneTreeTimer
@onready var jump_debounce:SceneTreeTimer

#Movement
var target_speed:float
var dir:float
var coyote:bool = false
#Cam Shake
var current_rotation:Vector2
var target_rotation:Vector2
#Bob Variables
var bob_time:float = 0.0
var smoothed_amp:float
var bob_pos:Vector2

#Lock Mouse
func _ready():
	#Hide Mouse
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
	#Instatiate Timers
	coyote_timer = get_tree().create_timer(0)
	jump_buff = get_tree().create_timer(0)
	jump_debounce = get_tree().create_timer(0)

#Inputs
func _input(event):
	#Quitting
	if event.is_action_pressed("quit"):
		get_tree().quit()

#Movement
func _physics_process(delta:float) -> void:
	#Get Direction
	dir = Input.get_axis("left", "right")
	target_speed = walk_speed
	#Movement
	move_and_slide()
	if is_on_floor():
		ground(delta)
	else:
		air(delta)

	#Jumping
	if Input.is_action_just_pressed("jump"):
		if is_on_floor() or coyote and coyote_timer.time_left > 0:
			jump()
		else:
			jump_buff = get_tree().create_timer(jump_buff_amount)
	#Jump Buffering
	if jump_buff.time_left > 0 and is_on_floor():
		jump()

	#Update Speed GUI
	#speed_gui.text = str(round(get_real_velocity().length()))

func ground(delta):
	#Ground Accel and Deccel
	velocity.x = move_toward(velocity.x, dir * target_speed, (ground_acel if dir else ground_decel) * delta)
	#Coyote Time Reset
	if velocity.y > 0:
		coyote = false
	else:
		coyote = true


func air(delta):
	#Gravity
	velocity.y += gravity * delta
	#Movement
	velocity.x = move_toward(velocity.x, dir * target_speed, (air_acel if dir else air_decel) * delta)
	#Coyote Time Process
	if coyote and coyote_timer.time_left <= 0:
		coyote_timer = get_tree().create_timer(coyote_time_amount)
	coyote_timeout()


#Jump Function
func jump():
	if jump_debounce.time_left <= 0:
		velocity.y = -jump_power
		velocity.x += jump_boost * dir
		coyote = false
		jump_debounce = get_tree().create_timer(jump_cool)


#Head Bob Function
func headbob(delta) -> void:
	var clamped_velocity:float = min(get_real_velocity().length(), walk_speed*1.5)
	#Smoothing The Sine Waves Position
	var smoothed_pos:Vector2 = lerp(cam.transform.origin, bob_pos * clamped_velocity * float(is_on_floor()) if dir else Vector2.ZERO, 0.01)
	#Sine Wave
	smoothed_amp = move_toward(smoothed_amp, bob_amp/10, delta/2) * float(is_on_floor())
	bob_pos.y = sin(bob_time * bob_freq) * smoothed_amp
	bob_pos.x = cos(bob_time * bob_freq/2) * smoothed_amp
	#Process
	bob_time += delta * velocity.length()
	cam.transform.origin = smoothed_pos

#Coyote Timeout
func coyote_timeout():
	await coyote_timer.timeout
	coyote = false

See if moving move_and_slide() after helps, maybe adding types to func ground(delta: float) -> void:. Make sure your ground_decel isn’t negative.

1 Like

Sorry to bother you again, but in my code I print ground_decel and its 0, this is the only script in my game, and no where in the code is it setting it to 0, do you know why it would be 0, its set to 6000

Its weird because this same code worked in 3D perfectly fine…

Maybe your instanced scene has a different value. It is an @export so it can change from scene to scene.

2 Likes

Maybe use a setter for it so you can set a breakpoint whenever it gets set; then you can see in the debugger if it starts as 0 or gets set to 0 somehow

1 Like

I’m not sure if this is related, but the change doesn’t appear to be at run time. I haven’t tested what you said, but I remember manully setting the ground_decel to 500 within the class defining script. This worked, but for some reason using the export feature fails to work.