0 votes

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: https://imgur.com/a/4jQRDts
What I want to do, but with the ball: https://youtu.be/rO19dA2jksk?t=377

Godot version 3.2.3 stable
in Engine by (55 points)

1 Answer

+1 vote
Best answer

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

by (4,476 points)
selected by

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

I tried to do what you advised and it didn't solve my problem. The game crashes immediately again :( 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 :(

My structure and code in the platform:
https://imgur.com/a/OagOLv5

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

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 :)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.