# Car AI help

**URL:** <https://forum.kirupa.com/t/car-ai-help/233064>\
**Category:** programming\
**Created:** [July 19, 2007, 8:16pm UTC](https://forum.kirupa.com/t/car-ai-help/233064 "2007-07-19T20:16:31Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![DrRobot](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/drrobot/32/3907_2.png) [@DrRobot](https://forum.kirupa.com/u/DrRobot)\
**Post date:** [July 19, 2007, 8:16pm UTC](https://forum.kirupa.com/t/car-ai-help/233064/1 "2007-07-19T20:16:31Z")

</div>

Ok, so i am working on a car game, and i have soem AI here, makes the car go to the waypoints, but, when the car gets to the third waypoint. it starts to twist and turn, then it straightens out.

the script:

onClipEvent (load) {  
var wayPointX:Array = new Array(“344”, “10”, “18”, “346”);  
var wayPointY:Array = new Array(“242”, “231”, “448”, “453”);  
var newRot:Number = 0;  
var mySpeed = 0;  
var myfSpeed:Number = 1.1;  
var myrSpeed:Number = .9;  
var curSpot = 0;  
var turnSpeed = 3;  
}  
//onEnterFrame = function(){  
onClipEvent (enterFrame) {  
if (curSpot\>wayPointX.length-1) {  
curSpot = 0;  
}  
xDiff = this.\_x-wayPointX[curSpot];  
yDiff = this.\_y-wayPointY[curSpot];  
distance = Math.sqrt(Math.round(xDiff_xDiff+yDiff_y Diff));  
newRot = Math.atan2(yDiff, xDiff)_(180/Math.PI);  
mySpeed += myfSpeed;  
difference = newRot-\_rotation-90;  
if (difference\>60) {  
//myfSpeed = .8;  
//turnSpeed = 6;  
} else {  
//myfSpeed = 1.1;  
//turnSpeed = 3;  
}  
if (distance\<=150) {  
myfSpeed = distance/175;  
} else {  
myfSpeed = 1.1;  
}  
//if (this.\_rotation\<difference) {  
//this.\_rotation -= turnSpeed_mySpeed/7;  
//}  
//if (this.\_rotation\>difference) {  
//this.\_rotation += turnSpeed_mySpeed/7;  
//}  
this.\_rotation += difference/2+turnSpeed;  
this.\_y += Math.cos(this.\_rotation_(Math.PI/180))_m ySpeed_-1;  
this.\_x += Math.sin(this.\_rotation\*(Math.PI/180))\*m ySpeed;  
if (mySpeed\<=0.3 && mySpeed\>=-0.3) {  
mySpeed = 0;  
}  
mySpeed \*= .91;  
if (distance\<=10) {  
curSpot += 1;  
}  
}

also, i’d like it to rotate as it moves., not rotate and then move
