AI - turning

My AI car are turning 180 degrees when I press play. I have used this simple code but don’t know what the problem is.

var target : Transform;
var moveSpeed : float = 3;
var rotationSpeed : float = 3;
var myTransform : Transform;

function Awake(){ myTransform = transform;

function Start(){ target = GameObject.FindWithTag("Player").transform;

function Update () { var lookDir = target.position - myTransform.position;
lookDir.y = 0; // zero the height differnece 
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(lookDir), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;

can someone help?

The Quaternion.LookRotation() and LookAt() are using the forward and up vectors of your character. Forward is facing the positive Z axis and Up is the positive Y axis. If you have the camera at the devault -10 on the Z axis looking towards +Z, you should be looking at the back of your character with a rotation of (0,0,0).

You can fix this problem in a variety of ways. You can go back to your authoring program and turn your character around. You can attach your script to an empty game object and then make your character a child of the empty game object. Then you can turn the character around as a child, leaving the empty game object rotation alone.

You might also be able to play games with the vector(s) you pass to LookRotation().