S
speedysilwady
Guest
Hi everyone, I am having trouble making my traffic AI work. Was wondering if anybody had any suggestions as to what I did wrong.
Its a top down driving game and all the vehicles spawn above the room and drive downwards through four lanes. Basically this code determines if there is another traffic object above or below or to the right or left of the object as well as any road barriers, and how the object should behave. I wanted the object to switch lanes if it was below another car. If there are no lanes available I wanted it to maintain a distance between the car below it.
If it is below a car I want the traffic object to slow down and maintain a distance as well.
Its a top down driving game and all the vehicles spawn above the room and drive downwards through four lanes. Basically this code determines if there is another traffic object above or below or to the right or left of the object as well as any road barriers, and how the object should behave. I wanted the object to switch lanes if it was below another car. If there are no lanes available I wanted it to maintain a distance between the car below it.
If it is below a car I want the traffic object to slow down and maintain a distance as well.
Code:
// STEP EVENT
if !instance_place(self.x,y-160,oTraffic) || !instance_place (self.x,y+64, oTraffic) {slowing=0;self.switching=0;}//no traffic above or below, then keep moving
if (instance_place(self.x,self.y-160,oTraffic)) {self.switching=1;} //If theres traffic above me self.switching = 1
if (instance_place (self.x,self.y+64, oTraffic)) {self.switching=2;} //If theres traffic below me self.switching = 2
switch (self.switching) {
case 1:
{
if !instance_place(self.x+1,self.y-160,oBarrier) && !instance_place(self.x-65,self.y-160,oBarrier)
{//if traffic is above me then i need switch to another lane
if !instance_place (self.x+32,self.y-160,oTraffic) && !instance_place (self.x-96,self.y-160,oTraffic)
{hspeed+=choose(20,-20);image_blend =c_red;} //free lane to the left and right of me, pick a lane
if instance_place (self.x+32,self.y-160,oTraffic) && !instance_place (self.x-96,self.y-160,oTraffic)
{hspeed-=20;image_blend = c_lime;} // free lane to the left of me, move left
if instance_place (self.x-96,self.y-160,oTraffic) && !instance_place (self.x+32,self.y-160,oTraffic)
{hspeed+=20;image_blend = c_lime;} // free lane to the right of me, move right
else {swiching=2;}
}
}
case 2:{
hspeed=0;vspeed=5;slowing=1;image_blend=c_black;}
case 0: {
if !(instance_place (self.x,self.y+64, oTraffic)){
if abs(oPlayer.vspeed<8 && oPlayer.vspeed>0){self.vspeed=oPlayer.vspeed*tmult1;hspeed=0;image_blend=c_blue;}
else {self.vspeed=tmult2;hspeed=0;}
}
else switching=2;
}
}