- Home /
AI cars model made by me in my car racing game not picking way point correctly and not moving
AI cars model made by me in my car racing game not picking way point correctly and not moving as i used a car racing game script from asset store and the dummy AI cars working perfect and moving along the way points (way points spread all over the track to guide the AI cars) now i am putting my models and attached with them AI Car script and assigned them the way points but they are not moving some time getting -ve acceleration and moving backward direction and truing on wrong direction ..!! is there some scaling factor that does matter as i have imported my car models fbx from maya 3D and the set there scale to somewhere like x=22,y=22,z==22 .. and then the size of my model cars equal to the dummy car .. but dummy car scale is x=1,y=1,z=1 can any one please help ...?? so i can proceed with my game ..!! as when i start game the AI script attached to my car models start showing current way pomint is 1,2,3..6 (total are 12) but the dummy model script show number of those way point in which the car is ..!!
Imagine that I answer smething like this where I write a lot without a proper layout and real long sentences that seem to never stop but also no real information about the problem (but also using some parenthesis to indicate lower importance information) and then back again but still no code that would help everyone to figure out what is wrong and sometime use google traduction to getting wrong traduction that make it even more hard for understand what you are trying to say but see in the middle I can stick some simili code like for waypoint you have if(distance < range) ChangeWaypoint(); .
Seriously...
below is the script and it is working perfect on dummy car model but not on the cars i have designed physics of my cars are perfect as i amusing them for player cars and they are working perfect so please .. try to understand and if you can help then please // ----------- CAR TUTORIAL SA$$anonymous$$PLE PROJECT, ? Andrew Gotow 2009 -----------------
// Here's the basic AI driven car script described in my tutorial at www.gotow.net/andrew/blog.
// A Complete explaination of how this script works can be found at the link above, along
// with detailed instructions on how to write one of your own, and tips on what values to
// assign to the script variables for it to work well for your application.
// Contact me at $$anonymous$$axwelldoggums@Gmail.com for more information.
// These variables allow the script to power the wheels of the car.
var FrontLeftWheel : WheelCollider;
var FrontRightWheel : WheelCollider;
var IsPause = true;
var ReduceSpeed : float;
// These variables are for the gears, the array is the list of ratios. The script
// uses the defined gear ratios to deter$$anonymous$$e how much torque to apply to the wheels.
var GearRatio : float[];
var CurrentGear : int = 0;
// These variables are just for applying torque to the wheels and shifting gears.
// using the defined $$anonymous$$ax and $$anonymous$$in Engine RP$$anonymous$$, the script can deter$$anonymous$$e what gear the
// car needs to be in.
var EngineTorque : float = 600.0;
var $$anonymous$$axEngineRP$$anonymous$$ : float = 3000.0;
var $$anonymous$$inEngineRP$$anonymous$$ : float = 1000.0;
private var EngineRP$$anonymous$$ : float = 0.0;
// Here's all the variables for the AI, the waypoints are deter$$anonymous$$ed in the "GetWaypoints" function.
// the waypoint container is used to search for all the waypoints in the scene, and the current
// waypoint is used to deter$$anonymous$$e which waypoint in the array the car is ai$$anonymous$$g for.
var waypointContainer : GameObject;
private var waypoints : Array;
private var currentWaypoint : int = 0;
// input steer and input torque are the values substituted out for the player input. The
// "NavigateTowardsWaypoint" function deter$$anonymous$$es values to use for these variables to move the car
// in the desired direction.
private var inputSteer : float = 0.0;
private var inputTorque : float = 0.0;
function Start () {
waypointContainer = GameObject.FindGameObjectWithTag("WayPoints");
// I usually alter the center of mass to make the car more stable. I'ts less likely to flip this way.
rigidbody.centerOf$$anonymous$$ass.y = -1.5;
IsPause = false;
ReduceSpeed = 1;
// Call the function to deter$$anonymous$$e the array of waypoints. This sets up the array of points by finding
// transform components inside of a source container.
GetWaypoints();
}
function Update () {
if(!IsPause)
{
// This is to limith the maximum speed of the car, adjusting the drag probably isn't the best way of doing it,
// but it's easy, and it doesn't interfere with the physics processing.
rigidbody.drag = rigidbody.velocity.magnitude / 250;
// Call the funtion to deter$$anonymous$$e the desired input values for the car. This essentially steers and
// applies gas to the engine.
NavigateTowardsWaypoint();
// Compute the engine RP$$anonymous$$ based on the average RP$$anonymous$$ of the two wheels, then call the shift gear function
EngineRP$$anonymous$$ = (FrontLeftWheel.rpm + FrontRightWheel.rpm)/2 * GearRatio[CurrentGear];
ShiftGears();
// set the audio pitch to the percentage of RP$$anonymous$$ to the maximum RP$$anonymous$$ plus one, this makes the sound play
// up to twice it's pitch, where it will suddenly drop when it switches gears.
audio.pitch = $$anonymous$$athf.Abs(EngineRP$$anonymous$$ / $$anonymous$$axEngineRP$$anonymous$$) + 1.0 ;
// this line is just to ensure that the pitch does not reach a value higher than is desired.
if ( audio.pitch > 2.0 ) {
audio.pitch = 2.0;
}
// finally, apply the values to the wheels. The torque applied is divided by the current gear, and
// multiplied by the calculated AI input variable.
FrontLeftWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * inputTorque ;
FrontRightWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * inputTorque;
if(FrontRightWheel.motorTorque>60 * ReduceSpeed)
FrontRightWheel.motorTorque=60*ReduceSpeed;
if(FrontLeftWheel.motorTorque>60*ReduceSpeed)
FrontLeftWheel.motorTorque=60*ReduceSpeed;
// the steer angle is an arbitrary value multiplied by the calculated AI input.
FrontLeftWheel.steerAngle = 10 * inputSteer;
FrontRightWheel.steerAngle = 10 * inputSteer;
}
}
function ShiftGears() {
// this funciton shifts the gears of the vehcile, it loops through all the gears, checking which will make
// the engine RP$$anonymous$$ fall within the desired range. The gear is then set to this "appropriate" value.
if ( EngineRP$$anonymous$$ >= $$anonymous$$axEngineRP$$anonymous$$ ) {
var AppropriateGear : int = CurrentGear;
for ( var i = 0; i < GearRatio.length; i ++ ) {
if ( FrontLeftWheel.rpm * GearRatio[i] < $$anonymous$$axEngineRP$$anonymous$$ ) {
AppropriateGear = i;
break;
}
}
CurrentGear = AppropriateGear;
}
if ( EngineRP$$anonymous$$ <= $$anonymous$$inEngineRP$$anonymous$$ ) {
AppropriateGear = CurrentGear;
for ( var j = GearRatio.length-1; j >= 0; j -- ) {
if ( FrontLeftWheel.rpm * GearRatio[j] > $$anonymous$$inEngineRP$$anonymous$$ ) {
AppropriateGear = j;
break;
}
}
CurrentGear = AppropriateGear;
}
}
function GetWaypoints () {
// Now, this function basically takes the container object for the waypoints, then finds all of the transforms in it,
// once it has the transforms, it checks to make sure it's not the container, and adds them to the array of waypoints.
var potentialWaypoints : Array = waypointContainer.GetComponentsInChildren( Transform );
waypoints = new Array();
for ( var potentialWaypoint : Transform in potentialWaypoints ) {
if ( potentialWaypoint != waypointContainer.transform ) {
waypoints[ waypoints.length ] = potentialWaypoint;
}
}
}
function NavigateTowardsWaypoint () {
// now we just find the relative position of the waypoint from the car transform,
// that way we can deter$$anonymous$$e how far to the left and right the waypoint is.
var tempWaypoint : Transform = waypoints[currentWaypoint] ;
var RelativeWaypointPosition : Vector3 = transform.InverseTransformPoint( Vector3(
tempWaypoint.position.x,
transform.position.y,
tempWaypoint.position.z ) );
// by dividing the horizontal position by the magnitude, we get a decimal percentage of the turn angle that we can use to drive the wheels
inputSteer = RelativeWaypointPosition.x / RelativeWaypointPosition.magnitude;
// now we do the same for torque, but make sure that it doesn't apply any engine torque when going around a sharp turn...
if ( $$anonymous$$athf.Abs( inputSteer ) < 0.5 ) {
inputTorque = RelativeWaypointPosition.z / RelativeWaypointPosition.magnitude - $$anonymous$$athf.Abs( inputSteer )- $$anonymous$$athf.Abs(Random.Range(0.1,0.4)) ;
Debug.Log(this.gameObject.name+" "+ waypoints[currentWaypoint].ToString());
}else{
inputTorque = 0.0;
}
// this just checks if the car's position is near enough to a waypoint to count as passing it, if it is, then change the target waypoint to the
// next in the list.
if ( RelativeWaypointPosition.magnitude < 20 ) {
currentWaypoint ++;
if ( currentWaypoint >= waypoints.length ) {
currentWaypoint = 0;
}
}
}
i think this is the problem if(distance < range) ChangeWaypoint() can anyone please see my code and corect me .. ? or explain these line if(distance < range) ChangeWaypoint() in term of my code
Answer by alee.xaib · Jul 31, 2013 at 12:07 PM
Problem Solved thanks for unity3D community for not helping me..
the problem was here RelativeWaypointPosition.magnitude < 20 due to scale of car the magnitude decreases .. i changed it to RelativeWaypointPosition.magnitude < 1 and it worked .. can any one please explain it to me . ??
This is not a good attitude bro.
Problem Solved thanks for unity3D community for "not helping me"