Curve3D.get_curve_offset gives offset based off player distance to world origin rather than curve(i think?) how to fix?

Godot Version

4.8.dev7 originally 4.8.dev4(updated just in case that was the issue)

How do i make get_closest_offset() get the offset from the curve to the player?

The gist of it is this:
im making an on a rail camera using curves, path follow and get_closest_offset() but the offset seems to be based on how close the player is to (0,0) I think, which causes the camera to start at the end/middle of its path depending on the curves length and how far from (0,0) the player traveled video demonstrating it here in a post i made for godot subreddit, git hub at bottom

I say i think because i did a test with two linear path cameras, one going length ways another width and they had very different offsets despite being right next to each other. (in the initial test length one being by roughly 14 and width roughly 6)

this is the code i use to get the camera to move on the curve:

path_follow.progress = curve.get_closest_offset(player_position)

git hub here

The when what where and why of it:
i asked a similar question on the godot subredditand i havent gotten an answer and honestly this is really confusing me. What i’m trying to do is create a “on a rail” camera for a small horror game project. Im thinking similar style to the alley-way scene silent hill or the forest scene in silent hill 2. The player walks and the camera moves along a set path alongside the players movement

This is what the camera is doing, progressing on a path i setup based on the closest offset of the players position to the curve, i have multiple cameras in the scene so i also have a collision box a player can trigger that will switch to that camera, which is why i cant just have it at (0,0)
here’s the code at its simplest and what im currently using to debug:

path_follow.progress = curve.get_closest_offset(player_position)

I set it up and it seemingly worked but if i placed it anywhere else i got this issue where the camera would start all the way at the end of its path or in the middle and after hours of messing with it and debugging i figured out that it starts at a varying offset depending on where you setup the camera.

If i setup the camera at (0,0) and have the player trigger it it works just as intended and the camera starts at the beginning of its path and progresses alongside the player. if I have it anywhere else the camera will have moved based on how far the player has moved from (0,0) For example i have a camera set up at (-16.804, 2.504, 0.0) and when the player triggers the camera switch, get_closest_offset(player_position) prints roughly 16 and ticks down the further away from the camera i get, finally getting to its starting position when i presume the player is around or before 0.0; but, sometimes, like i mentioned in the tl:dr, it doesn’t seem to be based off of the world origin and it really confuses me, so i think its based off of the world origin but im not entirely sure.

I am only about a month in of consistent programming so im aware i use functions of things and such without completely understanding so it has to be me using get_closest_offset() incorrectly but for the life of me i just cant figure out how. The documentation says “Returns the closest offset to to_point” so shouldnt it be returning the offset of the player to the curve? where the curve is located shouldnt be a factor besides how close it is to the player but idk I cant figure it out (◞‸◟,) its kinda bumming me out because i had milestones and everything then i got job interviews and a new job and got super behind on my project just to find this big ass bug i cant fix lol

I do have a repository here on github if anybody wants to look at it, i added some labels for controls and the specific camera i am testing out to make it easier to hop in, the main scene i am working in is “playground”

I also did find this post and tried it out because i thought maybe it had to do with some weird coordinate stuff, and maybe it does, but the solution proposed didn’t work for me, i kept getting the offset regardless

Currently i’m trying to find a work around by setting the curve progression to the distance between the player and the camera and its.. a work in progress. I’m not familiar enough with the math equations of why things happen the way they do to debug that good enough but i am trying (˶ㅠ︿ㅠ) I feel bad always asking questions online to strangers but i feel like im hitting these walls constantly and i feel like i dont have the vocabulary to properly describe these issues. a lot of the time i can find something online but the other half of it it seems i cant find anything because i dont know exactly what im looking for.

It would be best for me to use get_closest_offset() because it seems to be what im trying to do with the camera and i dont want to reinvent the wheel when theres a method already made for something like this but i cant figure out how to use it right.

I appreciate any help i can get! i hope this wasnt too wordy but i tried to be very descriptive to avoid confusion, thats why i included a tldr ha ha. again i really appreciate the help and time spent reading this (˶ㅠ︿ㅠ)

You forgot to read the full description of Curve.get_closest_offset() in the documentation.

Curve3D.get_closest_offset(to_point: Vector3)

[…] to_point must be in this curve’s local space.

What this means is, you have to transform the player’s position into the curve’s local space. If you don’t know what that means, you should read a bit about space transformations. It is an important concept to understand in game programming.

Here is what you basically need to do:

# Transform the player position into the curve's local space
var pos_local_to_curve = curve.global_transform.inverse() * player_position
# Use local position to compute curve offset
var offset = curve.get_closest_offset(pos_local_to_curve)
# Use offset for PathFollow3D
path_follow.progress = offset

It’s just the first line that you need to focus on. I can’t really explain it in words that would make it make sense right now. In essence, you’re making the position be relative to the curve’s origin and rotation instead of the world’s origin (and rotation). Try watching 3blue1brown’s video on Linear Transformations and Matrices.


I hope that helps you get on the right track. Let me know if you have any other questions.

THANK YOU SO MUCH! i’m looking into these as soon as i can, and sorry for the late reply I was asleep. I really appreciate the time you took to write out a solution and recommend a source to learn more about it, i was thinking about global vs local position but it wasnt really clicking in me, again thank you so much now that I know what I’m looking for hopefully I can go pretty far!!!