Godot Version
4.2.2
Question
In my game, the generated platforms move downwards. When the player is on the floor of these platform, he bounces back and forth on the moving, since the platform first move downwards (here the player is still in the air so it look like there is a small space between the floor and player, as seen in gif 2 below.) and then the player moves down due to gravity, Therefore it is not considered to be on the floor and the player dosent jump. Is there a solution for this?
What should happen even if the platform is moving (player can jump on input when on floor):
What is happening (Player cannot jump on input when the floor is moving down):
The moving platforms are proceduarrly generated scenes made up of tile maps with a physics layer and script just says it to change position.y with a certain speed. The player Code is below :
extends CharacterBody2D
var Speed = 300.0
var direction = 1
@export var jump_height : float = 250
@export var jump_time_to_peak : float = 0.45
@export var jump_time_to_descent : float = 0.39
@onready var jump_velocity : float = ((2.0 * jump_height) / jump_time_to_peak) * -1.0
@onready var jump_gravity : float = ((-2.0 * jump_height) / (jump_time_to_peak * jump_time_to_peak)) * -1.0
@onready var fall_gravity : float = ((-2.0 * jump_height) / (jump_time_to_descent * jump_time_to_descent)) * -1.0
@onready var ray_cast_right = $RayCastRight
@onready var ray_cast_left = $RayCastLeft
var wall_stick : bool = false
func _physics_process(delta):
if ray_cast_right.is_colliding() or ray_cast_left.is_colliding():
if velocity.y < 0:
velocity.y = 0
if Input.is_action_just_pressed("Touch") and not is_on_floor():
if ray_cast_left.is_colliding() or ray_cast_right.is_colliding():
direction *= -1
jump()
if ray_cast_right.is_colliding() and is_on_floor():
direction = -1
if ray_cast_left.is_colliding() and is_on_floor():
direction = 1
position.x += Speed * direction * delta
if not is_on_floor():
if (ray_cast_left.is_colliding() or ray_cast_right.is_colliding()):
velocity.y += get_gravity() * 0.15 * delta
elif (ray_cast_left.is_colliding() or ray_cast_right.is_colliding()) and velocity.y > 0:
velocity.y += get_gravity() * 0.3 * delta
else:
velocity.y += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("Touch") and is_on_floor():
jump()
move_and_slide()
func get_gravity() -> float:
return jump_gravity if velocity.y < 0.0 else fall_gravity
func jump():
velocity.y = jump_velocity