My vehicle gets launched into the space at the lightning speed

Godot Version

4.1.3

Question

Hello! I am programming a feature where you can drive a vehicle.
You come up to the vehicle, interact with it to get in, you drive somewhere, and then you interact again to get out. My problem occurs when getting out.

To allow player drive with the vehicle, I attach player node to the vehicle and turn off players collision so that there is no collisions between the vehicle itself and player inside it:

  • Player.cs::Interact()
// Disable player collision
GetNode<CollisionShape3D>("CollisionShape3D").Disabled = true;
// Reattach player from main scene to the vehicle node (so that vehicle and player inside it travel along)
GetParent().RemoveChild(this);
ControlledVehicle.DriverSocket.AddChild(this);
// Reset position
Position = Vector3.Zero;

When player gets out of the vehicle, I pretty much perform the same steps but in the opposite order

  • Player.cs::Interact()
Node3D controlledVehicleNode = (Node3D)ControlledVehicle;
// Reattach player back from vehicle to the main scene (player and vehicle are now not together)
ControlledVehicle.DriverSocket.RemoveChild(this);
controlledVehicleNode.GetParent().AddChild(this);
// Move player above the vehicle (for testing, just to be shure that vehicle and player don't intersect)
Position = controlledVehicleNode.Position + Vector3.Up * 5;
// Enable player collision back
GetNode<CollisionShape3D>("CollisionShape3D").Disabled = false;

Now, even tho I first move player far from vehicle and only then enable collision, vehicle suddenly violently is getting launched in some random direction, just like when it happens when you enable collision inside the vehicle.
The code I showed you is getting called in the _Process() player method, when action interact is pressed:

if (Input.IsActionJustPressed("interact"))
    Interact();

I am sitting on this for few days now, will be glad if someone could help me with this.
Thank you for your time!

Found related issue: Internal collision position doesn't update immediately when changing a CharacterBody's CollisionShape position. · Issue #67168 · godotengine/godot · GitHub
Also found a workaround commented on this issue, basically enabling collision after some time using tween: Internal collision position doesn't update immediately when changing a CharacterBody's CollisionShape position. · Issue #67168 · godotengine/godot · GitHub

1 Like