Can't set the platform as a parent for the player (ball)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By BodaMat

Hello! I’m creating a ball platformer, but I meet one problem. When a player gets on the platform and the platform starts to move, the ball starts to roll, and it hards to control. I see in unity someone use transform.parent and set ball child to the platform and ball move with the platform. I try to use the same with Godot, but the game crash when I reparent ball. I use this line of code:

ball.get_parent().remove_child(ball)
platform.add_child(ball)

,
What would you, please, advise to solve this problem?

What I have now: What I have now - Album on Imgur
What I want to do, but with the ball: https://youtu.be/rO19dA2jksk?t=377

:bust_in_silhouette: Reply From: Wakatta

Crash? How? Your code looks correct, its only that when you use add_child() the ball gets placed at Vector.ZERO so save the transform before remove_child() and reapply it after add_child()

var ball_trans = ball.get_global_transform()
ball.get_parent().remove_child(ball)
platform.add_child(ball)
ball.set_global_transform(ball_trans)

Since the Ball is a RigidBody expect some movement still
You may want to experiment with putting it to sleep or changing modes during input and none input states

It crashes after I add a child to the platform. The game closes immediately. Maybe the problem with transform or a camera. Because the ball has the camera as a child. I’ll try

BodaMat | 2021-02-28 12:07

I tried to do what you advised and it didn’t solve my problem. The game crashes immediately again :frowning: Maybe this is a bug?
State or mode don’t solve this problem, because the ball doesn’t copy platform position and the ball just stay in the same position :frowning:

My structure and code in the platform:
Platform and ball structure - Album on Imgur

BodaMat | 2021-02-28 13:33

Interesting indeed even with the camera as a child it should’t cause a crash from this.
Something else is afoot here.

Try using Break points and the Step into (F11) command to see exactly which line causes that issuse

Wakatta | 2021-02-28 14:40

Very very thank you. Debugger really helps. The problem was the ball enter the area twice and Godot can’t remove it from a child, because the ball hasn’t been reparented yet. I add if statement at the top _on_PlatformTrigger_body_entered function:

if body.get_parent() == platform:
	return

Now the camera is set to position 0 and I can’t move the ball. Maybe this is another topic :slight_smile:

BodaMat | 2021-03-01 18:17