# Constrain a transform to path?

Hello,

is there a way to constrain an object so that it is always on the path and that it’s distance from the car varies smoothly based on car.speed?

Thank you for any help with this

Hi

You can constrain a point to the path by simply using linear interpolation with Vector3.Lerp. All points in the path can be accessed using Path.vectorPath.

I am not totally sure why you want the distance from the car to be the speed of it, that would mean that if the car’s speed decreased, it would momentarily increase in actual speed since it would have to move closer to the point it is following…

Hello Aron,

thank you for the info. Would you be willing to write me how to do that in C#? I’m not that proficient in coding.

The reason for me wanting to set the distance from the car’s speed is because the faster the car goes the sooner it needs to steer. I’m trying to implement a slidey aggressive steering behaviour and to achieve that the car AI needs to anticipate what’s coming ahead depending on it’s travel speed.
To explain better, here’s a link to a Gif I couldn’t upload to this forum:

For testing purposes, I’ve coded it in a dirty way - though the results are inconsistent. Especially when the speed fluctuates around 50 or 100Kmh - the upcomingCorner position keeps popping back and forth:

int upcomingCorner = mCurrentCorner + 1;

if(mCar.speedKMH > 50.0f && mCar.speedKMH <= 100.0f)
upcomingCorner = mCurrentCorner + 2;

if(mCar.speedKMH > 100.0f)
upcomingCorner = mCurrentCorner + 3;

For now I’ve been able to smooth the position of the steering target like this:

steerTarget.position = Vector3.MoveTowards(steerTarget.position, upcomingCorner, mCar.speedKMH * 0.005f);

Thought it was possible to project a trajectory of a precise distance in meters along the Path and constrain the steerTarget at it’s end (like in the first pictures posted).
Any way to set the max length of the Path in meters (not in node count)?

Hi

You can do something like

``````/** Returns the point which is #distanceFromStart units along the path.
* FindPointOnPath(path, 0) will return the first point of the path
* FindPointOnPath(path, 5.8) will return a point 5.8 units from the start of the path (traveling along the path, not necessarily a straight distance)
*/
public Vector3 FindPointOnPath (List<Vector3> path, float distanceFromStart) {
// Accumulative path length
float accum = 0;
for (int i = 0; i < path.Count-1; i++) {
float d = Vector3.Distance(path[i+1], path[i]);

// The desired point is on this segment
if (accum + d > distanceFromStart) {
// Figure out
float factorOnThisSegment = (accum + d - distance)/d;
return Vector3.Lerp(path[i], path[i+1], factorOnThisSegment);
}
accum += d;
}
return path[path.Count-1];
}
``````

Then you have to find how far the car has traveled on the path currently and then call this method with that distance + the speed of the car.

Hello Aron,

thanks a lot for your help! It’s working flawlessly

Just got stuck for a while because Path.vectorPath is an Array, not a List (possibly because I’m still using v3.1.2 ?)

Solved it for now with: list = array.ToList(); using System.Linq;

Cheers