• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ky13harbor9 · Jun 13, 2015 at 06:19 PM · physicscarsimulationvehicle

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;
 }

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tanoshimi · Jun 13, 2015 at 06:30 PM 0
Share

And you've got a key defined in your input manager called "Return"?

avatar image Ky13harbor9 · Jun 13, 2015 at 06:50 PM 0
Share

I used "Submit" ins$$anonymous$$d. It's what the Input$$anonymous$$anager said to use, but still nothing happens...

avatar image Silent_Wrangler · Jun 13, 2015 at 07:00 PM 0
Share

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...

avatar image Ky13harbor9 · Jun 13, 2015 at 07:16 PM 0
Share

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

avatar image Ky13harbor9 · Jun 14, 2015 at 12:07 AM 0
Share

Looks like no buttons work anymore :/

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);
     }
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

fast forward physic vehicles 0 Answers

Vehicle programming source 5 Answers

Need for speed like physics using Edy's vehicle physics? 1 Answer

How can i import a new car in edy's vehicle physics? 2 Answers

Why does my car re-align after drift into the original direction? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges