I have an AI car and a player car, but when the camera is focused on the player car, the AI car looks like it is jerking, and when the camera is focused on the AI car, the player car is jerking. I even switch the camera and target during runtime, and still get the same results
Here is a video of what us happening. This is an android build
[Dropbox - Error - Simplify your life][1]31211_144231.mp4
Edited to add the script :
// ----------- CAR TUTORIAL SAMPLE 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 Maxwelldoggums@Gmail.com for more information.
// These variables allow the script to power the wheels of the car.
var FrontLeftWheel : WheelCollider;
var FrontRightWheel : WheelCollider;
var RearLeftWheel : WheelCollider;
var RearRightWheel : WheelCollider;
var Slow : System.Boolean = false;
// These variables are for the gears, the array is the list of ratios. The script
// uses the defined gear ratios to determine 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 Max and Min Engine RPM, the script can determine what gear the
// car needs to be in.
var EngineTorque : float = 600.0;
var MaxEngineRPM : float = 3000.0;
var MinEngineRPM : float = 1000.0;
private var EngineRPM : float = 0.0;
// Here's all the variables for the AI, the waypoints are determined 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 determine which waypoint in the array the car is aiming for.
var waypointContainer : GameObject;
var waypoints : Transform[];
private var currentWaypoint : int = 0;
// input steer and input torque are the values substituted out for the player input. The
// "NavigateTowardsWaypoint" function determines 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 () {
rigidbody.centerOfMass.y = -0.5;
GetWaypoints();
}
function Update () {
// 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 determine the desired input values for the car. This essentially steers and
// applies gas to the engine.
NavigateTowardsWaypoint();
// Compute the engine RPM based on the average RPM of the two wheels, then call the shift gear function
EngineRPM = (RearLeftWheel.rpm + RearRightWheel.rpm)/2 * GearRatio[CurrentGear];
ShiftGears();
// set the audio pitch to the percentage of RPM to the maximum RPM plus one, this makes the sound play
// up to twice it's pitch, where it will suddenly drop when it switches gears.
audio.pitch = Mathf.Abs(EngineRPM / MaxEngineRPM) + 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.
if(Slow){
if(rigidbody.velocity.magnitude*2.2369362912 >=45){
RearLeftWheel.motorTorque = EngineTorque / 3.2 *600* inputTorque;
RearRightWheel.motorTorque = EngineTorque / 3.2 *600* inputTorque;
}else{
RearLeftWheel.motorTorque = EngineTorque / 3.2 *-400* inputTorque;
RearRightWheel.motorTorque = EngineTorque / 3.2 *-400* inputTorque;
}
}else{
RearLeftWheel.brakeTorque = 0;
RearRightWheel.brakeTorque = 0;
RearLeftWheel.motorTorque = EngineTorque / 3.2 *-400* inputTorque;
RearRightWheel.motorTorque = EngineTorque / 3.2 *-400* inputTorque;
}
// the steer angle is an arbitrary value multiplied by the calculated AI input.
FrontLeftWheel.steerAngle = 35 * inputSteer;
FrontRightWheel.steerAngle = 35 * inputSteer;
}
function BrakeZone( Setting : String){
if(Setting == "Enter"){
Slow = true;
}else{
Slow = false;
}
}
function ShiftGears() {
// this funciton shifts the gears of the vehcile, it loops through all the gears, checking which will make
// the engine RPM fall within the desired range. The gear is then set to this "appropriate" value.
if ( EngineRPM >= MaxEngineRPM ) {
var AppropriateGear : int = CurrentGear;
for ( var i = 0; i < GearRatio.length; i ++ ) {
if ( RearLeftWheel.rpm * GearRatio *< MaxEngineRPM ) {*
-
AppropriateGear = i;* -
break;* -
}* -
}* -
CurrentGear = AppropriateGear;* -
}*
-
if ( EngineRPM <= MinEngineRPM ) {*
-
AppropriateGear = CurrentGear;* -
for ( var j = GearRatio.length-1; j >= 0; j -- ) {*
_ if ( RearLeftWheel.rpm * GearRatio[j] > MinEngineRPM ) {_
-
AppropriateGear = j;* -
break;* -
}* -
}* -
CurrentGear = AppropriateGear;* -
}*
}
function GetWaypoints () {
- var i :int = 0;*
- // 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*
- waypoints = new Transform[waypointContainer.GetComponentsInChildren(Transform).Length - 1];*
- for (var child : Transform in waypointContainer.GetComponentsInChildren(Transform)) {*
-
// do whatever you want with child transform here*
if(child.name.Contains(“WP”) || child.name.Contains(“00”)){
waypoints = child;
i++;
* }*
//print(child.name);
* }*
currentWaypoint = 0;
}
function NavigateTowardsWaypoint () {
* // now we just find the relative position of the waypoint from the car transform,*
* // that way we can determine how far to the left and right the waypoint is.*
* var RelativeWaypointPosition : Vector3 = transform.InverseTransformPoint( new Vector3(*
* waypoints[currentWaypoint].position.x,*
* transform.position.y,*
* waypoints[currentWaypoint].position.z ) );*
* Debug.DrawLine(transform.position, waypoints[currentWaypoint].position); *
* // 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) * 1.5 ;_
* // 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 ( Mathf.Abs( inputSteer ) < 0.5 ) {*
* inputTorque = RelativeWaypointPosition.z / RelativeWaypointPosition.magnitude - Mathf.Abs( inputSteer );*
* inputTorque = (RelativeWaypointPosition.z / RelativeWaypointPosition.magnitude - Mathf.Abs( inputSteer ));*
* }else{*
* }*
* // 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;*
* }*
* //Reset Waypoint to 0 (null factor)*
* }*