- Home /
Why won't my car start?
This is a basic simulation of a Volvo 850 (FWD)
The Code is Java, but when I press Return, nothing happens to the ignition variable.
Also no sounds ever play.
EDIT: Now no buttons work at all- not even steering which was working perfectly.
EDIT2: I've rebuilt the script, but I am still having some issues with buttons and axes not working and lines of code that stop working. For example, Return will work to set Ignition to 2, but the EngineRPM stays at 0. No other buttons or axes will work at this point.
Feel free to use the fixed code once it is.
var EngineStart : AudioClip;
var EngineSound : AudioClip;
var EngineStop : AudioClip;
var EngineRPM = 0;
var Ignition = 0;
var BrakeAxis = 0.0;
var BrakeStrength = 2000;
var RPMLimiter = 1;
var Transfer = 0;
var GearRatio = 0;
var FinalDrive = 3.5;
var Throttle = 0.0;
var RoadRPM = 0;
var PowerMult = 1;
var RedLineRPM = 6500;
var IdleRPM = 800;
var StallRPM = 500;
var EngineFriction = 0.015;
var WheelFL : WheelCollider;
var WheelFR : WheelCollider;
var WheelRL : WheelCollider;
var WheelRR : WheelCollider;
var WheelFLTrans : Transform;
var WheelFRTrans : Transform;
var WheelRLTrans : Transform;
var WheelRRTrans : Transform;
function Update () {
//ENGINE
if (Input.GetKeyDown(KeyCode.Return)){
if (EngineRPM < StallRPM){
Ignition = 2;
}
}
if (Input.GetKeyUp(KeyCode.Return)){
if (EngineRPM > StallRPM){
Ignition = 1;
}
else if (EngineRPM < StallRPM){
Ignition = 0;
}
}
if (Input.GetKeyDown("K")){
if (Ignition >= 1){
EngineSound.Stop();
EngineStop.Play();
}
Ignition = 0;
}
if (EngineRPM > RedLineRPM){
Limiter = 0;
}
if (EngineRPM < RedLineRPM - 200){
Limiter = 1;
}
if (Input.GetAxis("Vertical") > 0) {
Throttle = Input.GetAxis("Vertical");
}
else if (Input.GetAxis("Vertical") <= 0){
Throttle = 0;
}
var EngineTorque = PowerMult * (10000 + (EngineRPM - ((EngineRPM - 300) / 350) ^ 3)) / 79 + (EngineRPM * EngineFriction) ;
if (EngineRPM > StallRPM){
EngineRPM = EngineRPM + EngineTorque * Limiter * Throttle ;
}
if (EngineRPM < IdleRPM && EngineRPM > StallRPM && Ignition >= 1){
EngineRPM = EngineRPM + 35;
}
if (EngineRPM > 0){
EngineRPM = EngineRPM - (EngineRPM * EngineFriction);
}
if (EngineRPM < StallRPM){
EngineRPM = EngineRPM - 25;
}
if (Ignition == 2){
if (EngineRPM < StallRPM){
EngineRPM = EngineRPM + 50;
}
EngineStart.Play();
}
if (Ignition == 1 && EngineRPM > StallRPM){
EngineStart.Stop();
EngineSound.Play();
}
if (EngineRPM < 0){
EngineRPM = 0;
}
//TRANSMISSION
if (Input.GetKeyDown("1")){
GearRatio = 4;
}
if (Input.GetKeyDown("2")){
GearRatio = 3;
}
if (Input.GetKeyDown("3")){
GearRatio = 2;
}
if (Input.GetKeyDown("4")){
GearRatio = 1;
}
if (Input.GetKeyDown("5")){
GearRatio = 0.7;
}
if (Input.GetKeyDown("`")){
GearRatio = 0;
}
if (Input.GetKeyDown("r")){
GearRatio = -3;
}
AirRPM = (WheelFL.rpm + WheelFR.rpm) * 0.5 * GearRatio * FinalDrive;
Transfer = (EngineRPM - AirRPM);
WheelFR.motorTorque = Transfer * 0.01 * GearRatio * FinalDrive;
WheelFL.motorTorque = Transfer * 0.01 * GearRatio * FinalDrive;
EngineRPM = EngineRPM - Transfer;
//MISC
if (Ignition >= 1){
WheelFL.steerAngle = 40 * Input.GetAxis("Horizontal");
WheelFR.steerAngle = 40 * Input.GetAxis("Horizontal");
}
WheelFLTrans.Rotate(WheelFL.rpm / 60 * 360 * Time.deltaTime, 0, 0);
WheelFRTrans.Rotate(WheelFR.rpm / 60 * 360 * Time.deltaTime, 0, 0);
WheelRLTrans.Rotate(WheelRL.rpm / 60 * 360 * Time.deltaTime, 0, 0);
WheelRRTrans.Rotate(WheelRR.rpm / 60 * 360 * Time.deltaTime, 0, 0);
WheelFLTrans.localEulerAngles.y= WheelFL.steerAngle;
WheelFRTrans.localEulerAngles.y= WheelFR.steerAngle;
}
And you've got a key defined in your input manager called "Return"?
I used "Submit" ins$$anonymous$$d. It's what the Input$$anonymous$$anager said to use, but still nothing happens...
Cause you need to use Input.Get*Button*Down("Return") if there is such input axis name(Edit -> Project Settings -> Input) or Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Return) Hope this helps! About the sound: It seems you have no object with "Audio Listener" script on your scene so you can't hear anything...
No luck yet, Silent Wrangler. I've tried both of your suggestions, but unfortunately I have not been able to make either work. Also I do have an audio listener on my camera, which I forgot about. Either I'm doing this wrong or there's some bug in the system. Probably the first :l
Answer by Silent_Wrangler · Jun 13, 2015 at 07:19 PM
Cause you need to use Input.Get*Button*Down("Return") if there is such input axis name(Edit -> Project Settings -> Input) or Input.GetKeyDown(KeyCode.Return) Hope this helps! About the sound: It seems you have no object with "Audio Listener" script on your scene so you can't hear anything...
Answer by Ky13harbor9 · Jun 16, 2015 at 05:38 AM
Found out that java is kind of glitchy in many ways. I rebuild the code again in C and it's working beautifully.
Well, by beautifully I mean there's no steering visually or suspension travel visually, but it works!
using UnityEngine;
using System.Collections;
public class VolvoController : MonoBehaviour {
public float EngineRPM = 0;
public float Ignition = 0;
public float BrakeAxis = 0;
public float BrakeStrength = 500;
public int Limiter = 1;
public float Transfer = 0;
public float GearRatio = 0;
public float FinalDrive = 3.7f;
public float Throttle = 0;
public float RoadRPM = 0;
public float TorqueMult = 1;
public float RedLineRPM = 6500;
public float IdleRPM = 800;
public float StallRPM = 400;
public float EngineFriction = 0.01f;
public AudioClip EngineStart;
public AudioClip EngineSound;
public AudioClip EngineStop;
public WheelCollider WheelFL;
public WheelCollider WheelFR;
public WheelCollider WheelRL;
public WheelCollider WheelRR;
public Transform WheelFLTrans;
public Transform WheelFRTrans;
public Transform WheelRLTrans;
public Transform WheelRRTrans;
// Update is called once per frame
void Update () {
//ENGINE
if (Input.GetKeyDown(KeyCode.Return)) {
if (EngineRPM < StallRPM){
Ignition = 2;
}
}
if (Input.GetKeyUp(KeyCode.Return)){
Ignition = 1;
}
if (Input.GetKey(KeyCode.K)){
if (Ignition >= 1){
//EngineSound.Stop();
//EngineStop.Play();
}
Ignition = 0;
}
if (EngineRPM > RedLineRPM){
Limiter = 0;
}
if (EngineRPM < RedLineRPM - 200){
Limiter = 1;
}
if (Input.GetAxis ("Vertical") > 0) {
Throttle = Input.GetAxis ("Vertical");
} else if (Input.GetAxis ("Vertical") <= 0) {
Throttle = 0;
}
float EngineTorque = TorqueMult * 250 + (EngineRPM * EngineFriction) ;
if (EngineRPM > StallRPM){
EngineRPM = EngineRPM + EngineTorque * Limiter * Throttle ;
}
if (EngineRPM < IdleRPM && EngineRPM > StallRPM && Ignition >= 1){
EngineRPM = EngineRPM + 35;
}
if (EngineRPM > 0){
EngineRPM = EngineRPM - (EngineRPM * EngineFriction);
}
if (EngineRPM < StallRPM){
EngineRPM = EngineRPM - 25;
}
if (Ignition == 2){
if (EngineRPM < StallRPM){
EngineRPM = EngineRPM + 50;
}
}
if (Ignition == 1 && EngineRPM > StallRPM){
//EngineStart.Stop();
//EngineSound.Play();
}
if (EngineRPM < 0){
EngineRPM = 0;
}
//TRANSMISSION
if (Input.GetKeyDown(KeyCode.Alpha1)){
GearRatio = 4;
}
if (Input.GetKeyDown(KeyCode.Alpha2)){
GearRatio = 3;
}
if (Input.GetKeyDown(KeyCode.Alpha3)){
GearRatio = 2;
}
if (Input.GetKeyDown(KeyCode.Alpha4)){
GearRatio = 1;
}
if (Input.GetKeyDown(KeyCode.Alpha5)){
GearRatio = 0.7f;
}
if (Input.GetKeyDown(KeyCode.N)){
GearRatio = 0;
}
if (Input.GetKeyDown(KeyCode.R)){
GearRatio = -3;
}
RoadRPM = (WheelFL.rpm + WheelFR.rpm) * 0.5f * GearRatio * FinalDrive;
Transfer = (EngineRPM - RoadRPM);
if (EngineRPM > StallRPM && GearRatio != 0) {
EngineRPM = EngineRPM - Transfer * EngineRPM / RedLineRPM * 0.5f;
}
WheelFR.motorTorque = 0.05f * Transfer * GearRatio * FinalDrive;
WheelFL.motorTorque = 0.05f * Transfer * GearRatio * FinalDrive;
//BRAKES
if (Input.GetAxis("Vertical") < 0) {
BrakeAxis = -Input.GetAxis("Vertical");
}
else if (Input.GetAxis("Vertical") >= 0){
BrakeAxis = 0;
}
WheelFL.brakeTorque = BrakeStrength * BrakeAxis;
WheelFR.brakeTorque = BrakeStrength * BrakeAxis;
WheelRL.brakeTorque = BrakeStrength * BrakeAxis;
WheelRR.brakeTorque = BrakeStrength * BrakeAxis;
//MISC
if (Ignition >= 1){
WheelFL.steerAngle = 40 * Input.GetAxis("Horizontal");
WheelFR.steerAngle = 40 * Input.GetAxis("Horizontal");
}
WheelFLTrans.Rotate(WheelFL.rpm / 60 * 360 * Time.deltaTime, 0, 0);
WheelFRTrans.Rotate(WheelFR.rpm / 60 * 360 * Time.deltaTime, 0, 0);
WheelRLTrans.Rotate(WheelRL.rpm / 60 * 360 * Time.deltaTime, 0, 0);
WheelRRTrans.Rotate(WheelRR.rpm / 60 * 360 * Time.deltaTime, 0, 0);
WheelFL.transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, WheelFL.steerAngle, transform.rotation.eulerAngles.z);
WheelFR.transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, WheelFR.steerAngle, transform.rotation.eulerAngles.z);
}
}
Your answer
Follow this Question
Related Questions
Getting contact force between two rigidbodies 1 Answer
Car goes a bit left when moving 1 Answer
Vehicle engine RPM without wheelcolliders? 1 Answer
How to make a car not slow down while turning? 2 Answers
Raycast align with ground normal 2 Answers