In this episode we are going to make our AI car to apply breaks on some certain points or sectors on the map.
AICarScript
BrakingSectorScript
Some stuff about the functions , used in this episode.
Script that was modified in this episode.
var centerOfMass : Vector3;
var path : Array;
var pathGroup : Transform;
var maxSteer : float = 15.0;
var wheelFL : WheelCollider;
var wheelFR : WheelCollider;
var wheelRL : WheelCollider;
var wheelRR : WheelCollider;
var currentPathObj : int;
var distFromPath : float = 20;
var maxTorque : float = 50;
var currentSpeed : float;
var topSpeed : float = 150;
var decellarationSpeed : float = 10;
var breakingMesh : Renderer;
var idleBreakLight : Material;
var activeBreakLight : Material;
var isBreaking : boolean;
var inSector : boolean;
function Start () {
rigidbody.centerOfMass = centerOfMass;
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();
Move();
BreakingEffect ();
}
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;
if (steerVector.magnitude <= distFromPath){
currentPathObj++;
if (currentPathObj >= path.length)
currentPathObj = 0;
}
}
function Move (){
currentSpeed = 2*(22/7)*wheelRL.radius*wheelRL.rpm * 60 / 1000;
currentSpeed = Mathf.Round (currentSpeed);
if (currentSpeed <= topSpeed && !inSector){
wheelRL.motorTorque = maxTorque;
wheelRR.motorTorque = maxTorque;
wheelRL.brakeTorque = 0;
wheelRR.brakeTorque = 0;
}
else if (!inSector){
wheelRL.motorTorque = 0;
wheelRR.motorTorque = 0;
wheelRL.brakeTorque = decellarationSpeed;
wheelRR.brakeTorque = decellarationSpeed;
}
}
function BreakingEffect (){
if (isBreaking){
breakingMesh.material = activeBreakLight;
}
else {
breakingMesh.material = idleBreakLight;
}
}
Script that was created in this episode.
#pragma strict
var maxBreakTorque : float;
var minCarSpeed : float;
function OnTriggerStay (other : Collider){
if (other.tag == "AI"){
var controlCurrentSpeed : float = other.transform.root.GetComponent(AICarScript).currentSpeed;
if (controlCurrentSpeed >= minCarSpeed){
other.transform.root.GetComponent(AICarScript).inSector = true;
other.transform.root.GetComponent(AICarScript).wheelRR.brakeTorque = maxBreakTorque;
other.transform.root.GetComponent(AICarScript).wheelRL.brakeTorque = maxBreakTorque;
}
else {
other.transform.root.GetComponent(AICarScript).inSector = false;
other.transform.root.GetComponent(AICarScript).wheelRR.brakeTorque = 0;
other.transform.root.GetComponent(AICarScript).wheelRL.brakeTorque = 0;
}
other.transform.root.GetComponent(AICarScript).isBreaking = true;
}
}
function OnTriggerExit (other : Collider){
if (other.tag == "AI"){
other.transform.root.GetComponent(AICarScript).inSector = false;
other.transform.root.GetComponent(AICarScript).wheelRR.brakeTorque = 0;
other.transform.root.GetComponent(AICarScript).wheelRL.brakeTorque = 0;
other.transform.root.GetComponent(AICarScript).isBreaking = false;
}
}
One small problem. When we set the Tag "AI" to the car, it does not work. We have to set that tag to the object that has a collider. In your example, it should be the:"Car_car_auv" inside the RivalCar.