Move Car offset with respect to Waypoint path (Unity Car AI Control script)

Hello All,

I am trying to move car offset from track. I have four lanes of road. The path is curved and loop. Please guide.

float tiltY = target.rotation.eulerAngles.y;

float offXGlobal = xoffset * Mathf.Cos(tiltY);
float offZGlobal = xoffset * Mathf.Sin(tiltY);

Debug.Log("Values are X Offset " + offXGlobal + " Z Offset " + offZGlobal + " Z Angle " + tiltY + “TargetID” + progressNum);

target.position = circuit01.GetRoutePoint(progressDistance + lookAheadForTargetOffset + lookAheadForTargetFactor * speed).position;
target.position = new Vector3(target.position.x - offXGlobal, target.position.y, target.position.z - offZGlobal);

If you have a vector that is the direction of the track at any given point, you can rotate that vector by 90 degrees (left or right), normalize it and multiply it by lane spacing to get the adjacent track lane.

Vector3 alongTrackDirection = ...

Vector3 offset = Quaternion.Euler( 0, 90, 0) * alongTrackDirection.normalized * LaneSpacing;
1 Like

If you have a forward vector3 you can get the right vector by doing the cross product of forward and Up, then just multiply right by the distance you want to be offset.

Vector3 right = Vector3.CrossProd(forward, Vector3.up);
1 Like

Hi Kurt, Thank you for your reply. I appreciate your help

Hi Megafiers, Thank you very much for your reply. I appreciate your help. I like the way you are preparing your assets. Those are really amazing.

I solved the issue with very simple trick. Just added one more empty component in target, which is few meters offset. And called same componnent into Car AI Control Target. It solved my problem.