# How can I detect a collision between any 2 points of a path/spline?

Hey all. I'm trying to put together a spline system for my project which is a 2.5D platformer where you move on a path in 3D space kinda like Kirby 64 or Klonoa. Right now it only works when the player starts out by touching a spline point (1 of many spheres in a nested game object that manages the spline). Part of the game involves the player being able to potentially leap off the spline path they are on and then be put onto another spline. What I'm trying to figure out though is how the player object could see another path from a distance and then through a raycast, actually know that it's hitting the line between any 2 points of the spline and then can move to the right position of that spline. Unity doesn't have colliders that I can create for lines between all the spheres/points of the spline. I think colliders even if they were generated might get messy too. I was also thinking about having a loop that iterates through all points of the spline and detects if a player has joined up between each of the points but that too could be costly with more splines in the scene. Does anyone have any ideas of how this could be implemented and simplified/not overengineered?

The pic shows that I have 2 splines. 1 is on the main line and then the other if the player is near should be picked up by some raycast from the player. the player should be able to hit/detect a line between the spheres on that outer platform/rectangle and then be able to join up.

**Answer** by streeetwalker
·
Sep 11 at 07:59 AM

Yes, you won't be able to do it with raycasts unless you create a collider for the spline segments.

One simple approach seems like a problem where you need to find where two lines intersect. From the picture it looks like that spline segments are straight lines. If so, finding the intersection is a simple equation.

If you are working in 2D ( 2.5 D is just due to your camera angle?) create a line segment in x and y on the same plane that the splines exist on, that originates from the player outward in the direction they are looking. The other end point of this line segment can be set at some distance from the player.

From there, you may need to iterate over each line segment in the spline. There will be some nuances to contend with, like if the player line intersects more than one line segment in the spline.

I'm not working with 2D components. Could this solution be tweaked to work with 3D Components? Would the line segments be generated by the spline script? Also could this get expensive because I will need to iterate over a nonfinite number of segments? Can you clarify some of this please. I think you're onto something here.

@SirKingKrab, if it is in 2D the intersection equations simplified. In 3D you don't know where to look, so you won't be able to cast a line out from the player to try to find an intersection. But you can find the intersection of line and a plane that is in the players zy axis (assuming the player looks in z forward).

Your not saying you have an infinite number of splines, or segments of splines, just that they are not predetermined, right?

So, just like we'd do if we were writing our own collision detection (which is really what we are doing) we narrow down the number of splines we need to check against the plane intersection. So, you can cast a box or sphere collider outward from the player in the direction they are looking and collect the spline points it hits (because you have colliders on those). This will give you a subset of splines you need to check, and with some finesse even narrow down which segments within a spline you need to check for plane-segment intersections.

You'd probably collect all the points that intersect with the plane, and then decide which point you need to deal with to move the player to based on their distance from the player or some other criteria.

All that is doable and probably not difficult once you work out the logic and the equations involved. I'm a hacker (not a cracker!) by nature, but if I were doing this I would 100% work this problem out on paper first.

To answer your question about the splines, the goal is to just plot down spheres and then have a parent script make the actual path by the hierarchy order. So it's predetermined when I'm in the editor and then at runtime, the logic kicks in to actually make that parent object and its children into the spline. I just don't know how big all the splines will necessarily be when I start making levels. That part is simple and that's what I've got working a while back.

@SirKingKrab, it just occured to me that you could simplify the problem to 2D by because any 3 point form a plane. For any given line segment on the spline, you can cast a line outward in the plane formed by the endpoints of a line segment and the player's position.

So in the my previous reply, instead of finding the intersection of a plane and a given line segment on your spline, you could use lines - you just need to make sure the line you draw outward from the player is in the plane so defined.

The other problems of narrowing down the number of segments to check against are the same.

I don't know if this approach will be an simpler in the end than checking against the intersection of a plane...

So instead of having a few points like I do, I instead need to draw out even more spheres with colliders to compensate?

Also I just thought of something that might actually line up with what you said.

what if I have 2 raycasts going in the left and right directions that can determine if the player at least hits one spline point if it's a new spline? ie. I have the player standing on 2 parallel platforms. One's in the BG, one's in the centerground, this 2 raycast object can run to the background and then once it hits a spline in the BG (or splines if the points are paralel, then it notifies the player of the spline it found and then we can get them over there at the point where both rays started from. If only one splinepoint is found because maybe the next spline point is at an angle and not parallel, then there could be a way to read off the spline object what the next point is so then that could potentially get the player on track by maybe just locking the player to a midpoint. But I'm also just a fan of keeping things simple so if the player can hop into the BG and FG at points, make the geometry and splines parallel and straight to keep the headaches low and save the Kirby 64/Klonoa type movement for other kinds of splines

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Raycast not detecting hit 4 Answers

Create a custom polygon collider based on overlapping ogjects 0 Answers

Raycast not consistently detecting collision. 1 Answer

Raycast on moving object? 2 Answers