AI : #3 – Steer Control

This is the third episode of the AI series in this episode I’ll be showing how to create a simple Steer control for AI Car.
Before Going ahead you need to know about Global and Local World Space.

In a world Space there should be a origin or (0,0,0) point , now every other object will have a position value respect to that origin.

  
But in local world space , a object is taken as origin.

Also i have used InverseTransformPoint , this function is used to convert a point from global world space to local world space inside of unity.

 
 
AICarScript
var path : Array;
var pathGroup : Transform;
var maxSteer : float = 15.0;
var wheelFL : WheelCollider;
var wheelFR : WheelCollider;
var currentPathObj : int;


function Start () {
GetPath();
}

function GetPath (){
var path_objs : Array = pathGroup.GetComponentsInChildren(Transform);
path = new Array();

for (var path_obj : Transform in path_objs){
if (path_obj != pathGroup)
path [path.length] = path_obj;
}
}


function Update () {
GetSteer();
}

function GetSteer(){
var steerVector : Vector3 = transform.InverseTransformPoint(Vector3(path[currentPathObj].position.x,transform.position.y,path[currentPathObj].position.z));
var newSteer : float = maxSteer * (steerVector.x / steerVector.magnitude);
wheelFL.steerAngle = newSteer;
wheelFR.steerAngle = newSteer;
}
Posted in: AI

Leave a Reply

Your email address will not be published. Required fields are marked *