Your code for itself works perfect. But if i put it in i dont now where in the car script
i now i have to learn a lot about scripting.
so here is the total script, i commented your code out so to let it still work…
// Approximate motor torque curve with a simple parabola:
// torque goes from motorMinTorque at zero RPM to motorMaxTorque at
// motorRpmRange/2 RPM then back to motorMinTorque at motorRpmRange.
// This is way off the real motors, but works good for a simple car.
var motorMinTorque = 400.0;
var motorMaxTorque = 1000.0;
var motorRpmRange = 1500.0;
// Brake and handbrake torques.
var brakeStrength = 100.0;
var handBrakeStrength = 400.0;
// Steering: wheels can turn up to lowSpeedSteerAngle when car is still;
// and up to highSpeedSteerAngle when car goes at highSpeed km/h.
// This is to decrease steering at high velocities so that playing with
// plain keyboard is possible.
var lowSpeedSteerAngle = 45.0;
var highSpeedSteerAngle = 10.0;
var highSpeed = 35.0;
// How much we move the mass center vertically.
var massCenterY = -0.1;
// How much velocity based down-pressure force we apply.
var downPressureFactor = 0.5;
// other motor parameters
// defaults for corvette
//var rpmToGearUp = 5000.0;
//var rpmToGearDown = 2500.0;
private var minRpm = 1500.0*0;
// In this project we don’t simulate transmission (I ran out of time!).
// Just use 1.0 gear ratio.
//var forwardGearRatios = [2.66, 1.78, 1.30, 1.00, 0.74, 0.50];
//var backwardGearRatio = -2.90;
private var gearRatio = 1.0;
var differentialRatio = 3.42;
// UI indicators
var uiSpeed : GUIText;
var uiMotorRpm : GUIText;
// All Wheel components down the hierarchy
private var wheels : Component[ ];
private var curMotorRpm : float = 0.0;
// ------------------------------------------------------------
//var carStarted = false;
//function Update() {
//if (!carStarted) {
//if (Input.GetKeyDown(“space”)) {
//carStarted = true;
//audio.PlayOneShot(carStartNoise);
//}
//}
//else {
//your driving code here
//}
//}
//
function Start()
{
// get all wheels
wheels = GetComponentsInChildren(Wheel);
// lower center of mass
rigidbody.centerOfMass = Vector3 (0.0, massCenterY, 0.0);
}
function FixedUpdate()
{
// calculate current speed in km/h
var kmPerH = rigidbody.velocity.magnitude * 3600/1000;
// space does handbrake
var handbrake = 0.0;
if( Input.GetButton(“Jump”) )
handbrake = handBrakeStrength;
// current wheels rpm
var wheelsRpm = ComputeRpmFromWheels();
// find maximum steer angle (dependent on car velocity)
var maxSteer = Mathf.Lerp( lowSpeedSteerAngle, highSpeedSteerAngle, kmPerH / highSpeed );
// steering
var steerAmount = Input.GetAxis (“Horizontal”);
var steer = steerAmount * maxSteer;
ApplyDownPressure();
// motor brake
var motor = 0.0;
var brake = 0.0;
var motorAxis = Input.GetAxis(“Vertical”);
var axisTorque = ComputeAxisTorque( Mathf.Abs(motorAxis) );
if( Mathf.Abs(motorAxis) > 0.1 ) // if any of up/down keys is pressed
{
if( wheelsRpm * motorAxis < 0.0 )
{
// user is trying to drive in the opposite direction - treat that as brake
brake = brakeStrength;
}
else
{
// user is driving in the same direction - treat as motor
motor = motorAxis * axisTorque;
}
}
// update all wheels
var relativeVelocity = Quaternion.Inverse(rigidbody.rotation) * rigidbody.velocity;
for( var w:Wheel in wheels )
{
w.UpdateWheel( handbrake, motor, brake, steer, relativeVelocity );
}
// update UI texts if we have them
if( uiSpeed != null )
uiSpeed.text = kmPerH.ToString(“f1”) + “\tkm/h”;
if( uiMotorRpm != null )
uiMotorRpm.text = curMotorRpm.ToString(“f0”) + “\trpm”;
// sound
audio.pitch = 0.7+0.9curMotorRpm/3000;
audio.volume = 0.1+0.9curMotorRpm/3000;
}
// ------------------------------------------------------------
private var smoothSidewaysSlip = 0.0;
private var smoothForwardSlip = 0.0;
private var maxDownPressureForce = -1500.0;
function ApplyDownPressure()
{
// apply down pressure to the car (only if any wheel is grounded)
//var wheelCount = wheels.Length;
var groundedCount = 0;
var sidewaysSlip = 0.0;
var forwardSlip = 0.0;
for( var w:Wheel in wheels )
{
if( w.collider.isGrounded )
{
++groundedCount;
var hit : WheelHit;
var wc : WheelCollider = w.collider;
wc.GetGroundHit(hit);
sidewaysSlip += hit.sidewaysSlip;
forwardSlip += hit.forwardSlip;
}
}
if( groundedCount ) {
sidewaysSlip /= groundedCount;
forwardSlip /= groundedCount;
}
var smoother = Time.deltaTime * 5.0;
smoothSidewaysSlip = Mathf.Lerp( smoothSidewaysSlip, sidewaysSlip, smoother );
smoothForwardSlip = Mathf.Lerp( smoothForwardSlip, forwardSlip, smoother );
if( groundedCount )
{
var downPressure = Vector3(0,0,0);
downPressure.y = -Mathf.Pow(rigidbody.velocity.magnitude, 1.2) * downPressureFactor;
downPressure.y = Mathf.Max( downPressure.y, maxDownPressureForce );
var localPos = Vector3(0,0,0);
localPos.x = smoothSidewaysSlip / 5.0;
localPos.x = -Mathf.Sign(localPos.x) * localPos.x * localPos.x;
var hwidth = collider.size.x / 2.0;
localPos.x = Mathf.Clamp( localPos.x, -hwidth, hwidth );
var worldPos = rigidbody.position + rigidbody.rotation * localPos;
downPressure = transform.TransformDirection( downPressure );
rigidbody.AddForceAtPosition( downPressure, worldPos, ForceMode.Acceleration );
}
}
// A simple parabola to approximate the shape of motor torque
// curve. In real cars you probably would do this with explicit
// table that maps rpm to torque.
function MotorTorqueCurve( motorrpm : float )
{
var x = (motorrpm/motorRpmRange) * 2 - 1;
var y = 1 - x * x;
return Mathf.Lerp( motorMinTorque, motorMaxTorque, y );
}
// Given acceleration pedal input, computes the torque that
// we want to apply to wheel axles.
function ComputeAxisTorque( accelPedal : float ) : float
{
// Compute current motor rpm, based on wheels rpm
var wheelRpm = Mathf.Abs( ComputeRpmFromWheels() );
ComputeMotorRpm( wheelRpm );
// Figure out motor torque based on motor rpm
var motorTorque = accelPedal * MotorTorqueCurve( curMotorRpm );
// Compute wheel axle torque from motor torque
return motorTorque / gearRatio / differentialRatio;
}
// Compute motor RPM from wheel RPM
function ComputeMotorRpm( rpm : float )
{
curMotorRpm = rpm * gearRatio * differentialRatio;
curMotorRpm = Mathf.Min( curMotorRpm, motorRpmRange );
}
// Compute average RPM of all motorized wheels
function ComputeRpmFromWheels() : float
{
var rpm = 0.0;
var count = 0;
for( var w:Wheel in wheels )
{
if( w.motorised )
{
rpm += w.collider.rpm;
++count;
}
}
if( count != 0 )
rpm /= count;
return rpm;
}