Hi,
I have a road mesh. I need to calculate the offset distance between centre of the road lane and car.
I have attached a reference image below. Please help me to solve this.
Thanks in advance.
Hi,
I have a road mesh. I need to calculate the offset distance between centre of the road lane and car.
I have attached a reference image below. Please help me to solve this.
Thanks in advance.
I am not 100 % sure, but you can try this:
var offset = lane.GetComponent<MeshFilter>().mesh.bounds.center.x - car.GetComponent<MeshFilter>().mesh.bounds.max.x;
Maybe something like this?
Math.Abs(carCentre.x - laneCentre.x) - (carWidth / 2.0)
The road is not straight road. So how do i do this?
That makes it a bit more complicated.
How is your road represented?
A spline or a model or something?
Do you want the distance to the edge of the car, or is it okay to use the transform center of the car?
Depend on how your lane is represented, I had to solve this previously, using a waypoint system. The basics was to find which are the closest waypoint, each waypoint have a reference to the next waypoint:
It all depend on which way you represent your spline, if you are using quadratic bezier spline you can use:
My lane is separate mesh with identical number. My road is two way multi-lane road. I can detect the current lane of my car. but its difficult to fine the lane offset. Actually I need to calculate lane offset(distance between car center and lane center) and road offset(distance between car center and road center). I really don’t have knowledge about way points.
Please help me with some sample code to solve this.
Thanks in advance.
How does the car follow the road?
Using raycast i can detect the current lane.
Yes but I mean how does the car know where to go next? How do it know the direction of the road?
Its the player car so the player control the car.
You need to represent the middle of the road in some way, if you want to get the distance to it. If it’s a winding road, there’s no way for Unity to know how it looks without you defining it.