# Racing: AI Player car turns wheels L/R but moves forward

Good Afternoon,

I am creating a racing game in Unity using a car racing tutorial I found on this forum as a basis/foundation. I am using a waypoint system for the AI to calculate its path. The agent does fine on straight lanes on the track, and turns the wheels accordingly on curves but still moves straight and flies directly off the map. I am confused as to why the character moves straight with its wheels turned. Here is the code snippet. Many of you may recognize it from a tutorial posted some time ago on this forum.

``````	// Calculate the target position relative to the this transforms coordinate system.
relativeTarget = transform.InverseTransformPoint (target);

// Calculate the target angle for the wheels, so they point towards the target
targetAngle = Mathf.Atan2 (-relativeTarget.z, relativeTarget.x);

// Atan returns the angle in radians, convert to degrees
// The wheels have a maximum rotation angle
targetAngle = Mathf.Clamp (steerMaxAngle, -steerMaxAngle, targetAngle);

// Apply the rotation to the wheels
// Wheels rotate around the y-axis
// The rotation has to be relative to the car, which is the transform parent of the wheels
frontLeftWheel.localEulerAngles = Vector3 (0, targetAngle, 0);
frontRightWheel.localEulerAngles = Vector3 (0, targetAngle, 0);
// Aso apply rotation to pod + L-Hand and R-Hand.
L_hand.localEulerAngles = Vector3 (0, targetAngle, 0);
R_hand.localEulerAngles = Vector3 (0, targetAngle, 0);
pod.localEulerAngles = Vector3 (0, targetAngle, 0);

if (hasWheelContact)
{
// Accelerate
transform.Translate (wheelForce, 0, 0);

// Another way to accelerate; currently does not work.

// We are too fast and need to turn too much. Slow down.
if (Mathf.Abs (targetAngle) > 2  rigidbody.velocity.magnitude > .08) {
// We are too fast
rigidbody.drag = 5;
}
}
``````

Also, is there a way to prevent the character from flying through boundaries (made with maya) on the map? I’ve tried making the subdivisions on the boundaries tighter as suggested by someone on this forum but this was not effective. This only seems to occur on curves and loops.

Thanks for the help. I know this was a long post, sorry.

Hmm… it does seems that you are applying the force, but not in the wheel direction… so the wheels do turn L/R, but the force you applied is still straight, and not rotated according to wheel angels.

For the boundaries, did you create a box collider (or enabled mesh collider, if you have clock cicles to waste, that is highly doubtfull) and allineated it to the boundaries’s meshes? Also, you have to enable the collider on the car.
Putting a mesh in the scene, is just this, the mesh, it does not come by default with a Physics collider.

Phil

Thank you for the speedy response. I apologize for not being more specific, the character has been tested with both wheel colliders and box colliders (box colliders seem to work better for some reason). I have also tried mesh a mesh collider added to the entire vehicle with no success.

The boundaries on the track have been broken into smaller sections to make more manageable, and a mesh collider has been added to each section.

I have also tried connecting all boundaries to be one unit in Maya and adding one mesh collider to the entire thing which didn’t work either.

Also, I understand what you are saying about applying force to the rotation. I tried multiple things with that with no success but will go back to it since this seems like the most logical explanation for the issue.

Thank you for the direction.

Hmm… tried to relplicate your situation, but got no errors… if you’d like me to give a shot to your project, upload it, other than this, i Don’t know what wouldn’t be working.

P.S: Friction is set right? if friction is disabled on the track’s surface it won’t steer

Phil

when using wheel colliders are you setting the front wheels to steerable?

I think I may have found a solution. First, it is important to know that the models XYZ is a little funky as it was designed in a bizarre way by the 3D modeler working on the project with me.

X = forward/backward
Y = rotation
Z = left/right

With this in mind, i want to apply the proper to the Z axis to make a turn relative to the degree of the turn.

Problem is I need to know the exact coordinates of the object every frame. How can I find this out? I cannot seem to figure it out using the scripting reference.

It would read something like this:

Zforce = (UpdatedCoordinates/targetAngle);

then:

transform.Translate (wheelForce, 0, ZForce);

So how can I figure the coordinates out?

Thank you all for your help and patience.

Also, the friction is set to 1 as specified in a Physics Material I made and added to the track.

Nevermind, got that all figured out now.

Now I just need to figure out how to calculate rise and run using the slope and degree. Once that’s figured, I can hopefully plug the run into the ZForce variable and it will work.

Will post back if this works as a reference to anyone who may find this thread during a search.

Thanks.