Closest position on a Spline to a Player

Hello,

My current project is a character action game with a camera that follows a track as opposed to being fixed in position relative to the character - as in this video: http://youtu.be/rrYcsxOXzkY?t=24m40s. In exactly the same way Spare Parts worked, I want to track the player character’s position in the world using a target spline (closest position along the spline that is), then communicate the result to another spline which acts as the track for a camera to follow. Any help as to the best methods of doing this in Blueprint would be greatly appreciated. I already have the Camera moving along a spline based on the character’s world position, but it’s not exactly what I want to do.

Thanks!

Even I am interested in a camera like this (similar to God Of War) if you wouldn’t mind sharing some pointers.

Here’s how i have done it.
I have created the spline and then taken an array of n no. of vectors. Then I divide the length of the spline by n. and then I store world position of points along the spline into my array by the function World Position Of Point Along Spline At Distance. Then at each frame I subtract each vector from the array from the player’s position. The least result gives me the position on the spline closest to the player. Then I interpolate the position of the camera to the resultant point on the spline. It needs tweaking in terms of interpolation speed and the no. of points I take along the spline,but it works quite well. Another optimization could be to check only specific points from the array instead of the whole array depending on triggers along the level.

Sounds like a good clean way of doing it! Would you mind posting a screenshot of your blueprint solution? :slight_smile:

Here i have attached the two functions which do the job.

  1. The SetPathPositions fills the path positions array with the required world positions along the spline at begin play.
  2. The GetClosestPosition finds the closest position from among the path positions array to the player.

Then you can use the resultant position with a Vinterpto to interpolate your camera with a desired speed.
(You will have to Tweak the no. of spline points and the camera speed to find a nice balance)
[1]:


[2]:

2 Likes

Thank you very much. I shall give this a go :slight_smile:

I tried this approach, and my camera is stuck at the origin of the world. After recreating the blueprint functions posted, i went ahead and set the camera location using those values. But the values are not updating. Not sure if i set this up right.

Is your spline path reference null or it could be a problem if you are attaching the camera component before detaching it maybe ?

I figured out what i did wrong. I needed to put a check on the set Array Elem node of the “get closest position” function. Even though that worked, i still cant get it to smoothly move through the spline :(. And also, i am trying to control this through the forward and right vectors of the player’s movement. Would you have any tips for that? What ive tried previously didnt quiet work.

Hey guys I had to tackle this issue a while back in UDK and wanted to create it in blueprint for UE4

Here is my solution to a fast and fairly accurate test for finding the closest point on a spline from the player location.

I use a recursive function that is called in the tick. It checks the start and end, gets the closest, then checks that against the mid point. It keeps doing it for N times or until its close enough, the user can change those vars based on each camera spline actor but around 10 times or less has always worked for me in my testing.

Local Variables I use here:

40776-splinerecursionlocals.png

How it looks in game:

[- YouTube][3]

I also ended up doing a lengthy tutorial on how to create the entire actor and set it up here if you are interested:

[http://gregmladucky.com/blog/?p=445#more-445][4]

Hopefully that helps!

1 Like

Any updates on this? I am trying to do the same thing, though I’m using a sphere that follows the player. I can get the sphere to jump to different points on the spline, but I’m having trouble getting it to move smoothly between them.

There is the node ‘find location closest to world location’. if you provide it the player’s world location you should get the closest point on the spline.

7 Likes

This was super useful. Thank you!

All this searching all over the internet and this tiny answer was it. Thank you lol

1 Like

It is 2023 now and to anyone finding this q&a please do not get dragged into the complexity shown above.
Spline Function ‘find location closest to world location’ is all you need. It is generous of the previous posters to share their solutions but they are completely unnecessary.
cheers
Podge.

1 Like

Is this in the context of Spline movement only (requires a Spline)? Please confirm.

Because Im in UE4.27 and using [Simple Move to Node]. And the question sounds like a problem I have with Epic’s node: it only makes the edge of the Player’s capsule Move to the Loc, but not the center of the Capsule.
Thus if Cap radius = 30, then the Location has an error of 30 units (very annoying, and I cant use AI nodes on Player).

In my experience the MoveTo node will use the players’ location and the acceptance radius to decide when the targetpoint has been reached. The edge of the capsule is not relevant.
I think, for a test you could print out your ai player location and the target point to see when the player stops moving.
Perhaps subtract target location from player location and print the length. Compare this with the acceptance radius.
print on each tick and you will find out what is going on.
cheers
Podge.

Hi, I’m using [Simple Move to Node] which does not have [Acceptance Radius], that is the problem. And it is Player, not AI, thus I cannot use the better Move Nodes that have settings (cant connect Player Controller on AI Controller nodes).

  • This has been asked before, I didnt see a solution (How to get the Player Controller temp controlled by an AI Controller).
  • Thus I was wondering if OP or ‘find location closest to world location’ was a solution for me in 2023. But my path is made up live, I have no Spline.

I see a bit clearer now. If you want a precise position that your character ends at you can now use motion warping in Ue5. So, I think you could use your simple MoveTo to get close to where you want to be and then play an anim montage (needed for warp) and warp yourself to the perfect position.

1 Like

Thanks. But Im still in 4.27. So your post shows more of a reason why we need to move to 5x :frowning:

  • But in 4.27, I use [Simple Move] - to get to the general location. Then I must use a [Set Location] - to get to the exact point (doesnt animate feet/jerks body, which I can detect in Slow motion). But there’s timing problems/no Return pin for [Simple Move] (i’ll discuss in another thread, Im working on code to share).