Hello everyone,
I started to use Unity 3D one month ago with the objective to create a realistic driving simulator for my researches. At the moment, i’m trying to create a script that returns me the vehicle lateral deviation from the road axis but i’ve some issues. In particular, i tried modelling the road axis like an invisible gameobject and i created a raycast that i attached to my vehicle to measure the distance from it to the axis.
The problems are two:
The vehicle may have a deviation in both the right and left direction with respect to the axis (during driving activity) so i need to set the ray not only in right or only in left direction, but both.
I need that the ray hit only the “axis gameobject” and that ignore all the other objects in the scene.
Have you some ideas to solve these problems?
This procedure that i described is very laborius but, at the moment, is the only solution that i have in mind. If you have some other ideas to measure the deviation beetween vehicle and axis during driving activity, i’ll accept all your advices.
I don’t exactly understand what you’re trying to achieve, but for the 2nd question you can create a Layer in the Project Settings and set this axis object to this Layer. Every user-defined Layer has a number from 8 to 31 and you can pass this number as the “layerMask” parameter in the Raycast method.
Thanks for your reply. In regard to the question 1) i’ll try to explain better my problem:
During driving activity the drivers follow an imaginary trajectory (that in general overlap the central axis of the road). A parameter much important for my research is the vehicle deviation (left and right) from this trajectory.
I supposed that best way to find this parameter is to insert a central object (that represent my axis) and measure the distance through raycast. I don’t know if there are other Unity commands that allow me to find this parameter in an easier way. I accept any suggestions.
Maybe you could create an collider which cuts the car horizontally (look at my beautiful paint skills: Screenshot by Lightshot) and always collides with the road axis. When using methods like OnCollisionStay, you can get the coordinates of the collision and then calculate the distance between it and your car.
I’m not sure though whether this is a better approach.