0

I'm making a stability measuring game for a specific controller. In this game you try to stay on the road as well as you can. (Steering is done by weight shifting of the body in standing position.) To measure how well player follows the road I need to make a curve that is on the exact middle of the road.
Then I need to have the shortest path (line) to said curve, so I can measure distance from the middle.

For now I have thought of a straight road solution - a constant rayCast looking for collision from the player to the plane. I could make it always look perpendicular to the plane using it's normal vector. However I have no Idea how to make that check for a Curve for example 90 degree with 5 unit radius. I would need to calculate the ray direction constantly somehow.

Straight Road with middle-Plain:

Straight Road with middle-Plain

90 degree turn:

90 degree turn

Do you know how to do that ?

Spektre
  • 49,595
  • 11
  • 110
  • 380
N_C_Pub
  • 1
  • 1
  • I found a potential solution involving the origin point of a curve. (Ray Should point always in that direction or alongside the radius to be oriented correctly ) – N_C_Pub May 16 '23 at 19:02
  • Sorry sir I do not understand the question. Maybe more diagrams would be helpful including examples of what correct output would look like. Also consider using splines with nodes that you can poll for their rotation at points of interest like this. – mrVentures May 16 '23 at 20:47
  • use cubic curves for the roads (and also for the desired path) so it look smooth , then you can compute distance like this [Is it possible to express "t" variable from Cubic Bezier Curve equation?](https://stackoverflow.com/a/60113617/2521214) otherwise just use perpendicular distance to line ... the score can be sum of the distances or max distance along the whole trip +/- scaled by speed – Spektre May 17 '23 at 07:09
  • Not directly a curve but you can have a look into the [Source code of `HandleUtilties`](https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/HandleUtility.cs) where you can find (and copy out) all kind of handy math utils you could adopt to your needs (eg. instead of mouse position stick to 3D positions etc) - can't use them directly since the class is in `UnityEditor` and not available in a build application ;) – derHugo May 17 '23 at 08:17
  • Also if a little bit of inaccuracy is fine then you could just to various raycasts (e.g. 4 or 8) into all directions and use the smallest one as distance ... if two yield very similar results you could even add one more in the middle between them to get a better result ... just instead a plane simply use a collider that is not colliding with your player or anything (layer collision matrix) but has purely the purpose to be hit by the raycasts – derHugo May 17 '23 at 08:20

0 Answers0