Can somebody please helmp me find error in this code, I can’t
// car physics calculations/input stuff
private var accel : Vector3;
public var throttle : float;
private var deadZone : float = .001;
private var myRight : Vector3;
private var velo : Vector3;
private var flatVelo : Vector3;
private var relativeVelocity : Vector3;
private var dir : Vector3;
private var flatDir : Vector3;
private var carUp : Vector3;
private var carTransform : Transform;
private var carRigidbody : Rigidbody;
private var engineForce : Vector3;
private var turnVec : Vector3;
private var imp : Vector3;
private var rev : float;
private var actualTurn : float;
private var carMass : float;
private var wheelTransform : Transform[] = new Transfcrm[4]; //these are the transforms for our 4 wheels
public var actualGrip : float;
public var horizontal : float; //horizontal input control, either mobile control or keyboard
private var maxSpeedToTurn : float = .2; //keeps car from turning until it's reached this value
// the physical transforms for the car's Theels
public var frontLeftWheel : Transform;
public var frontRightWheel : Transform;
public var rearLeftWheel : Transform;
public var rearRightWheel : Transform;
//these transform parents will allow wheels to turn for steering/separates steering turn from acceleration turning
public var LFWheelTransform : Transform;
public var RFWheelTransform : Transform;
// car physics adjustments
public var power : float = 300;
public var maxSpeed : float = 50;
public var carGrip : float = 70;
public var turnSpeed : float = 3.0; //keep this value somewhere between 2.5 and 6.0
private var slideSpeed : float;
public var mySpeed : float;
private var carRight : Vector3;
private var carFwd : Vector3;
private var tempVEC : Vector3;
function Start()
{
Initialize();
}
function Initialize()
{
// Cache a reference to our car's transform
carTransform = transform;
// cache the rigidbody for our car
carRigidbody = rigidbody;
// cache our vector up direction
carUp = carTransform.up;
// cache the mass of our vehicle
carMass = rigidbody.mass;
// cache the Forward World Vector for our car
carFwd = Vector3.forward;
// cache the World Right Vector for our car
carRight = Vector3.right;
// call to set up our wheels array
setUpWheels();
// We set a COG here and lover the center of'mass to a
//negative value in Y axis to prevent car from flipping over
carRigidbody.centerOfMass = Vector3(0,-0.7,.35);
}
function Update()
{
// call the function to start processing all vehicle physics
carPhysicsUpdate();
//call the function to see what input we are using and apply it
checkInput();
}
function LateUpdate()
{
// this function makes the visual 3d vheels rotate and turn
rotateVisualWheels();
//this is where we send to a function to do engine sounds
engineSound();
}
function setUpWheels()
{
if((null == frontLeftWheel || null == frontRightWhee1 || null == rearLeftWheel || null == rearRightWheel ))
{
Debug.LogError("One or more of the wheel transforms have not been plugged in on the car");
Debug.Break();
}
else
{
//set up the car's vheel transforms
wheelTransform[0] = frontLeftWheel;
wheelIransform[1] = rearLeftWheel;
wheelIransform[2] = frontRightWheel;
wheelIransform[3] = rearRightWheel;
}
}
private var rotationAmount : Vector3;
function rotateVisualWheels()
{
// front wheels visual rotation while steering the car
LFWheelTransform.localEulerAngles.y = horizontal * 30;
Rfwheelfransform.localEulerAngles.y = horizontal * 30;
rotationAmount = carRight * (relativeVelocity.z * 1.6 * Time.deltaTime * Mathf.Rad2Deg);
wheelTransform[0].Rotate(rotationAmount);
wheelTransform[1].Rotate(rotationAmount);
wheelTransform[2].Rotate(rotationAmount);
wheelTransform[3].Rotate(rotationAmount);
}
private var deviceAccelerometersensitivity : float = 2; //hov sensitive our mobile accelerometer vill be
function checkInput()
{
//Mbbile platform turning input...testing to see if ve are on a mobile device.
if (Application.platform == RuntimePlatform.IPhonePlayer ||(Application.platform == RuntimePlatform.Android)
{
// V8 give the acceleration a little boost to make turning more sensitive
accel = Input.acceleration * deviceAccelerometerSensitivity;
if(accel.x > deadZone || accel.x < -deadZone){
horizontal = accel.x;
}
else
{
horizontal = 0;
}
throttle = 0;
for (var touch : Touch in Input.touches) {
if(touch.position.x > Screen.width -Screen.width /3 touch.position.y < Screen.height /3)
{
throttle = 1;
}
else if(touch.position.x < Screen.width/3 touch.position.y < Screen.height/3)
{
throttle= -1;
}
}
}
else if (Application.platform.== RuntimePlatform.WindowsEditor || RuntimePlatform.WindowsWebPlayer || RuntimePlatform.WindowsPlayer )
{
//Use the Keyboard for all car input
horizontal = Input.GetAxis("Horizontal");
throttle = Input.GetAxis("Vertical");
}
}
function carPhysicsUpdate()
{
//grab all the physics info ve need to calc everything
myRight = carTransform.right;
// find our velocity
velo = carRigidbody.velocity;
tempVEC = Vector3(velo.x,0,velo.z);
// figure out our velocity vithout y movement - our flat velocity
flatVelo = tempVEC;
// find out vhich direction we are moving in
dir = transform.TransformDirection(carFwd);
tempVEC = Vector3(dir.x,0,dir.z);
// calculate our direction, removing y movement - our flat direction
flatDir = Vector3.Normalize(tempVEC);
// calculate relative velocity
relativevelocity = carTransform.InverseTransformDirection(flatVelo);
// calculate how much we are sliding (find out movement along our x axis)
slideSpeed = Vector3.Dot(myRight,flatVelo);
// calculate current speed (the magnitude of the flat velocity)
mySpeed = flatVelo.magnitude;
// check to see if we are moving in reverse
rev = Mathf.Sign(Vector3.Dot(flatVelo,flatDir));
// calculate engine force with our flat direction vector and acceleration
engineForce = ( flatDir * ( power * throttle ) * carMass);
// do turning
actualTurn = horizontal;
// if we're in reverse, we reverse the turning direction too
if(rev < 0.1f)
{
actualTurn =- actualTurn;
}
// calculate torque for applying to our rigidbody
turnVec =((( carUp * turnSpeed ) * actualTurn ) * carMass )* 800;
// calculate impulses to simulate grip by taking our right vector, reversing the slidespeed and
// multiplying that by our mass, to give us a completely ‘corrected’ force that would completely
// stop sliding. we then multiply that by our grip amount (which is, technically, a slide amount) which
// reduces the corrected force so that it only helps to reduce sliding rather than completely
// stop it
actualGrip = Mathf.Lerp(100, carGrip, mySpeed * 0.02);
imp = myRight * ( —slideSpeed * carMass * actualGrip);
}
function slowVelocity ()
{
carRigidbody.AddForce(-flatVelo * 0.8);
}
//this controls the sound of the engine audio by adjusting the pitch of our sound file
function engineSound ()
{
audio.pitch = 0.30 + mySpeed * 0.025;
if (mySpeed > 30)
{
audio.pitch = 0.25 + mySpeed * 0.015;
}
if (mySpeed > 40)
{
audio.pitch = 0.20 + mySpeed * 0.013;
}
if (mySpeed > 49)
{
audio.pitch = 0.15 + mySpeed * 0.011;
}
//ensures ve dont exceed to crazy of a pitch by resetting it back to default 2
if ( audio.pitch > 2.0 ) {
audio.pitch = 2.0;
}
}
function FixedUpdate()
{
if(mySpeed < maxSpeed)
{
// apply the engine force to the rigidbody
carRigidbody.AddForce( engineForce * Time.deltaTime );
}
//this controls the sound of the engine audio by adjusting the pitch of our sound file functicn engineSound 0
if (mySpeed > maxSpeedToTurn)
{
carRigidbody.AddTorque ( turnVec * Time.deltaTime );
}
else if(mySpeed < maxSpeedToTurn)
{
return;
}
// apply forces to our rigidbody fr grip
carRigidbody.AddForce( imp * Time.deltaTime );
}
Thanks