hi CommUnity "Starting a Car"

Happy new year to everyone…

As you now i do i kind of racing game, and scripting isn´t my strength, but i learned alot in this great engine tutorials…

So my question is, how can i make my car start by pressing for example the “space” key?

so my game starts, and my car should be “off” then after pressing “space” the car should be on and the “engine_startx.ogg” sound have to be played.

do i have to make the car.js be attached by pressing “space”?

thanks

That’s one way to do it. I would recommend a variable.

var carStarted = false;

function Update() {
if (!carStarted) {
if (Input.GetKeyDown("space")) {
carStarted = true;
audio.PlayOneShot(carStartNoise);
}
}
else {
//your driving code here
}
}

I will try it out tomorrow, incredibly fast reply…
thanks…
i saw your BotBuilder, great, i think you will have a quite good future :slight_smile:

Thanks! As luck would have it, I already do have a great-looking future - Eduweb, the guys doing Wolfquest recently asked me to join their team. :slight_smile:

…is it just me, or are there a lot of people named “ray” on this forum? (assuming I’m correctly interpreting your name as “slashdot ray”) There’s me, you, and also the guy from MantaRay Interactive.

Don’t forget "Raycast :slight_smile:

Hi starmanta,
sorry i just could not answer earlier.

i tried it now, but if i put it onto the car.js script of the racing game tutorial, i recieve an error.

i put it on the first line, then after the the else in the brackets the code of the car script begins.

and it did not work. editor says “expecting a EOL but { …”
or something like this…

so where do i have to place the snippet ?

thanks in advance. and sorry for not beeing a specialist until now.

the nickname ray is from a time 12 years ago then i started to work as an unix-SAP administrator on my first job so my best friend putted the /. on my name and since 6 years i´m /.ray

When it says “Expecting EOF but found }”, that means there was one too many closing brackets - since every bracket has to match another, you’re “closing” out of nothing, that generates the error. I’m guessing this is what the error is.

Does it still give the error if you include only the code I pasted (without your code in there)? It’s one way to work out where the problem is - what I pasted looks correct to me, but I might have missed something.

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.9
curMotorRpm/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;

}