How can I find the closest object in front of an object?

I scripted this car to go to a specific object on a specific Bezier curve. However, now I want this car to go to the closest object in front of the car on this specific curve.

Here’s an image of what I’m trying to achieve

So far, I only know how to look for the closest object, but this closest object could be ahead or behind the vehicle.

I have the position of the vehicle and have managed to get the steering to the object as well. All I’m trying to do is look for the closest object in front of the vehicle.

Any help is very much appreciated :slight_smile:.

Try to check distance from the actual front of the car
изображение

To check if a object is in front of the car, you can use vector3:Dot(). You will most likely want to check this near the front of the car, not the center.

Presumably you know the t value for your car’s position along the curve. Do you also have a t value for the other objects along the curve? Because the problem is fairly simple then, just find the lowest t value which is higher than your car’s.

If you don’t have those objects’ t values or can’t easily get them somehow, you’ll have to project the objects you’re interested in onto the closest point on the bezier and then check those.

graphics - Closest point on a cubic Bezier curve? - Stack Overflow Looks like it has some good resources for doing the projection if you need.