Search Unity

How to make Vehicle AI to change lanes

Discussion in 'Scripting' started by ar0nax, May 26, 2011.

  1. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    I am working on a project atm, and I need the vehicle AI to switch lanes. From lane 1 to lane 2 and vice versa. I made a script to make the vehicle stick to a lane, and in update i made it send a flag to the function (0 for first lane 1 for second lane) and make the car move towards that lane. but while moving towards the lane it starts to wobble when it reaches it..
    NOTE: the lanes are 2 concentric ellipses-like (it's a race track)
    I am using a waypoint in the middle of the track which moves along it's z axis as much as the vehicle. I calculate the distance from the waypoint to the vehicle and try too maintain it around a specified distance (band1, which is lane 1 and band2 which is lane2 and band1<band2)
    .

    I need to get rid of that wobble effect (it changes the steerAngle to make it keep the lane too much too fast)

    here's the code of the function:
    Code (csharp):
    1. function AutoSteer(flag){
    2.     var distance : float;
    3.     distance = Vector3.Distance(reference.transform.position, transform.position);
    4.     if(flag == 0){
    5.         if(distance > band1 + maxDistance){
    6.             colliderFL.steerAngle = MaxSteeringAngle * (band1 + maxDistance - distance) * 0.5;
    7.         }
    8.         else
    9.             if(distance < band1 + minDistance){
    10.                 colliderFL.steerAngle = MaxSteeringAngle * (band1 + minDistance - distance) * 0.5;
    11.         }
    12.         if(Mathf.Abs(colliderFL.steerAngle) > 10){
    13.                 if(distance > (band1 + band2)/2)
    14.                     colliderFL.steerAngle /= 8;//
    15.                 else
    16.                     colliderFL.steerAngle /= -10;
    17.         }
    18.     }
    19.     if(flag == 1){
    20.         if(distance > band2 + maxDistance){
    21.             colliderFL.steerAngle = MaxSteeringAngle * (band2 + maxDistance - distance) * 0.5;
    22.         }
    23.         else
    24.             if(distance < band2 + minDistance){
    25.                 colliderFL.steerAngle = MaxSteeringAngle * (band2 + minDistance - distance) * 0.5;
    26.         }
    27.         if(Mathf.Abs(colliderFL.steerAngle) > 10){
    28.                 if(distance < (band1 + band2)/2)
    29.                     colliderFL.steerAngle /= 8;
    30.                 else
    31.                     colliderFL.steerAngle /= -10;
    32.         }
    33.     }
    PS: I am kinda new to unity, and I tried to play around with the values.. with no luck. (all code was written by me, feel free to correct me, or offer a better solution) :p
     
  2. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    Here is an image that I hope will explain better what I am trying to do.


     
  3. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    i think using Ray from the reference object to the vehicle might do the trick, but i don't quite know how to use it, any help pls?
     
  4. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    bump.