Godot Version
v4.2.2.stable.official [15073afe3]
Background
I’m making a train game so my player is a PathFollow3D
that I use to follow Path3D
s in the level.
It’s possible for the player to switch tracks, and to do so I reparent()
them to another Path3D
Setup
I have a Player
scene that has a top level child CameraController
. The CameraController is top_level so I can manually lerp()
the position etc to smoothly follow the player.
I have a TitleScene
that instantiates my Level
and Player
scenes. The Player
then gets added to the Level
. This currently just involves adding the Player
as a child of a random Path3D
.
When the level loads, the camera is in the correct position behind the player. Following it also works as expected, hurrah
However, when I reparent the Player
, the camera suddenly jumps to the level “origin” (and then smoothly moves back to following the player)
What I think is happening
It seems like when the Player
is first added to the Level
, the CameraController
gets positioned relative to the Player
.
For example:
Say in the Player
scene the CameraController
is positioned (0, 1.5, 6)
.
The Player
is added to the Level
at position (-85, 0.25, 40)
Which gives the CameraController
a position of (-85, 1.75, 46)
Once I reparent()
the Player
the CameraController
suddenly realises its top level so “removes” the Players
position.
Here is a log of the CameraController
s position (without any lerping code moving it), when I just keep switching the Player
back and forth between two different parents.
(-86.00563, 1.75, -5.999998)
(-5.852806, 2, 3.41007)
(-70.56184, 2.25, 47.84929)
(-84.25036, 2.5, -9.260246)
(-47.54408, 2.75, -22.9677)
(-7.655383, 3, -4.111481)
(24.74981, 3.25, 13.84217)
As you can see the position isn’t even remaining constant after the first reparent, or switching between two different values. Which implies to me every reparent
call is changing the position for some reason. This is probably easiest to see with the y
value which is increasing by 0.25
every time (which is actually the Player
s vertical offset (a property on PathFollow3D))
What I did
I’ve saved the camera transform before I call reparent then set it back afterwards. This works but doesn’t feel correct…
Questions
- Is this expected behaviour, a bug?
- Am I doing something stupid?
- Is there a better/more correct way of fixing this?