Box floats after animated platform moves downwards

Godot Version

4.2

Question

I have a platform which I put a box on. I then click to have the platform move upwards, however, when the platform returns downwards it leaves the box floating.
image

if i use the player to click on the side of the box to pull it, the box will fall due to gravity.
How do i resolve this?

Sounds like your only applying physics on a collision.

Can you expand on that please?

Well it’s hard to say exactly without knowing anything about your box or its code, but since you said it falls once the player hits it it sounds like your only applying physics (ie gravity) on collision.


Setup above. Below is the code to pull the box onto the platform.

func _physics_process(delta):
	#if not on_platform_body:
		#velocity.y += gravity_scale * delta
	#else:
	velocity.y = 0
	velocity += platform_velocity * delta
		
		
	if Input.is_action_pressed("player_action") and (player_right_handle or player_left_handle):
		action_hold_time += delta
		#print("hold down key: ", action_hold_time)
		if action_hold_time >= HOLD_ACTION_THRESHOLD:
			if position.distance_to(player.position) > holding_distance:
				position = lerp(position, player.position, BOX_SPEED * delta )
	else:
		action_hold_time = 0.0

	position += velocity * delta

	pass

Purple collision box is to detect if its on the platform. It is also used to check for the player.

func _on_player_checker_body_entered(body):
	if body.is_in_group("Player"):
		#print("player_checker entered box area - PURPLE" )
		pass
	if body.is_in_group("LiftBody"):
		on_platform_body = true
		#platform_velocity = body.get_linear_velocity()
		print("on_platform_body entered: ", on_platform_body  )

The platform itself is just a moving AnimatableBody2D which moves up and down.
Hope this is more helpful.

1 Like

Yeah that’s great. Right off i see you’re adding rhe gravity if its not on the platform, but it’ all commented out. I’m guessing you did that because it wasn’t working as you expected? Probably because on_platform_body isn’t getting set back to false.

if not on_platform_body:
		velocity.y += gravity_scale * delta
	else:
		velocity.y = 0
		velocity += platform_velocity * delta

With this code enabled it does something odd. Now i do have a exit player check i piggyback on also like above.

func _on_player_checker_body_exited(body):
	if body.is_in_group("Player"):
		#print("on_platform_body: ", on_platform_body  )
		pass
	if body.is_in_group("LiftBody"):
		on_platform_body = false
		platform_velocity = Vector2.ZERO
		print("on_platform_body exited: ", on_platform_body )

So if i disable the top code I get the floating effect of the box. If it is enabled., it lets the platform drop a few pixels which are very obvious but lands on the platform and goes back down.

This the out put of the print statements.

on_platform_body entered: true ← fisttime box is pulled onto the platform
on_platform_body exited: false ← box floats
on_platform_body entered: true ← box drops and lands on platform
image
image
image

Not sure what its stuck on or why it should not just stay on the platform becoing TRUE till the player moves it again.
Cheers for sticking with me on this!

OK so I ended up trying it out because that didn’t seem to make sense, and I think I see what you’re talking about. It’s caused by you box going into sleep mode because it’s stopped moving (to save CPU) You can turn this off under Deactivation\can sleep. I also found that gravity’s automatically added to RigidBody2D’s so you shouldn’t need that.

Tried what you said. Worked. I did end up getting things working by adding a small script to the platform and calulating the velocity of the platform. Passed this through to the box and also worked.

Is it wise to turn of the sleep function on objects?

Nice!

If it’s just one box is probably not a big deal, but yeah you wouldn’t want to generally just turn it off. That was more to test it. You’d want to use the is_sleeping() and set_sleeping() methods.

1 Like
func _process(_delta):
	#stops the the cart floating at the top of the lift
	if on_platform_body:
		set_can_sleep(false)
	else:
		set_can_sleep(true)
	pass

Did what you said and now it makes for sense. Thanks for you help!

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