Im trying to create a tile in godot (2d) that changes the CharacterBody2D's up_direction in gd script. the idea is to move the player towards either up/down/left/right using changed gravity once they touch the specific tile. The code currently works for up/down direction, but not for left/right. When the CharacterBody touches a wall to the left or the right, is_on_floor() will always be false (should be true), why is this the case?

player.gd:
extends CharacterBody2D
const SPEED = 150.0
const JUMP_VELOCITY = -300.0
# const FLOOR_MAX_ANGLE = PI - PI/4
var gravity_dir = Vector2.DOWN
#var gravity_force = 400
# const GRAVITY = 10
# var velocity = Vector2.ZERO
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
#func get_gravity_variable() -> Vector2:
#return Variables.custom_gravity
func _physics_process(delta: float) -> void:
var direction := Input.get_axis("move_left", "move_right")
# self.floor_max_angle = FLOOR_MAX_ANGLE
print("floor max angle: ", self.floor_max_angle)
print("up direction: ", self.get_up_direction())
print("is on floor: ", is_on_floor())
print("is on wall: ", is_on_wall())
print("current grav: ", gravity_dir)
print("current velocity: ", velocity)
print("current direction: ", direction)
# Add the gravity.
if not is_on_floor():
velocity += gravity_dir * get_gravity() * delta
#velocity.y = (velocity.y + GRAVITY) * delta
_jump()
_move(direction)
_update_direction(direction)
_play_animations(direction)
move_and_slide()
func _update_direction(direction) -> void:
# update sprite facing based on input direction
# if (gravity_dir.x < 0):
# self.set_rotation(-PI / 2)
# elif (gravity_dir.x > 0):
# self.set_rotation(PI / 2)
# else:
if (direction > 0):
animated_sprite_2d.flip_h = false
elif direction < 0:
animated_sprite_2d.flip_h = true
if (gravity_dir.y < 0):
animated_sprite_2d.flip_v = true
elif (gravity_dir.y > 0):
animated_sprite_2d.flip_v = false
func _jump() -> void:
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
if (gravity_dir.x < 0 || gravity_dir.x > 0):
if (gravity_dir.x < 0):
velocity.x = -JUMP_VELOCITY
if (gravity_dir.x > 0):
velocity.x = JUMP_VELOCITY
else:
if (gravity_dir.y < 0):
velocity.y = -JUMP_VELOCITY
if (gravity_dir.y > 0):
velocity.y = JUMP_VELOCITY
func _move(direction) -> void:
# Get the input direction and handle the movement/deceleration.
if (gravity_dir.x < 0 || gravity_dir.x > 0):
# velocity.x = 0
if direction:
velocity.y = direction * SPEED
else:
velocity.y = move_toward(velocity.x, 0, SPEED)
else:
# velocity.y = 0
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
func _play_animations(direction) -> void:
# play animations
if is_on_floor():
if direction == 0:
animated_sprite_2d.play("default")
else: animated_sprite_2d.play("run")
else:
animated_sprite_2d.play("jump")
tile.gd:
extends Area2D
#const UP_DIRECTION = Vector2(0, 1)
#const DOWN_DIRECTION = Vector2(0, -1)
#const LEFT_DIRECTION = Vector2(-1, 0)
#const RIGHT_DIRECTION = Vector2(1, 0)
@export var selected_direction := Vector2(0, 1)
func _on_body_entered(body):
# body.velocity = 0.0
body.gravity_dir = selected_direction
body.set_up_direction(-selected_direction)
# print("selecteddir: ", selected_direction)
# print("updir: ", body.get_up_direction())