• 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
Question by LPGaming-Company · Feb 12, 2013 at 12:38 AM · transformaccelerationvehicle

Basic Acceleration Script

Trying to figure out how to get a basic acceleration script going for a car.

I have really slow internet so I can't download the Car Tutorial everyone recommends, but here is what I've got so far..

This works.. although it's kinda strange... it accelerates really slow for the first few seconds, then jumps to the max speed. Aswell as the car wont accelerate in 3rd gear+, it's stupid really. I don't see why.

 using UnityEngine;
 using System.Collections;
 
 public class MoveCar : MonoBehaviour {
     public float currSpeed;
     public float maxSpeed;
     
     public int currDamage;
     public int maxDamage;
     
     public int currRPM;
     public int redRPM;
     public int maxRPM;
     
     public int gear;
     public int maxGears;
     
     public float damagePerc;
     
     
     public bool reverse;
     public bool transAuto;
     
     // Use this for initialization
     void Start () {
         currSpeed=0f;
         maxSpeed=200f;
         currDamage=0;
         maxDamage=100;
         
         currRPM=0;
         redRPM=6150;
         maxRPM=7000;
         
         gear = 0;
         maxGears = 6;
         
         transAuto = true;
         reverse = false;
     }
     
     // Update is called once per frame
     void Update () {
         
         if(gear > 0 && (currRPM > redRPM) && transAuto) {
             gear++;
             currRPM-= (currRPM / 2);
         }
             
         
         switch(gear) {
             case 0: maxSpeed = 0; break;
             case 1: maxSpeed = 45; break;
             case 2: maxSpeed = 83; break;
             case 3: maxSpeed = 107; break;
             case 4: maxSpeed = 143; break;
             case 5: maxSpeed = 176; break;
             case 6: maxSpeed = 200; break;
         }
         if(Input.GetKeyUp(KeyCode.Q)) {
             if(gear < maxGears) {
                 gear++;
                 currRPM-= (currRPM / 2);
             }
         }
         
         if(Input.GetKeyUp(KeyCode.W)) {
             if(gear > 0) {
                 gear--;
                 currRPM+= (currRPM * 2);
             }
         }
         
         if(!reverse && currSpeed < 1) { currSpeed = 0; }
         if(reverse && currSpeed < -29) { currSpeed = -30; }
         
         if(Input.GetKey(KeyCode.UpArrow)) {
             if(currRPM < 0) currRPM=0;
             if(currRPM < maxRPM) {
                 currRPM +=  8;
             }
             if(gear > 0) {
                 if(currSpeed < maxSpeed) {
                     currSpeed += ((.0004f * currRPM) / gear);
                 }
             }
         } else {
             currSpeed-= (int)((1 * gear) / 2);
             currRPM-= (int)(3 * Time.deltaTime);
         }
         
         if(Input.GetKey(KeyCode.DownArrow)) {
             reverse = true;
         } else {
             reverse = false;
         }
             
           transform.Translate(Vector3.forward * (currSpeed * Time.deltaTime) / 2);
             Debug.Log ("Speed: " +currSpeed+ " RPM: " +currRPM+ " Gear: " +gear);
     }
 }
 
Comment

People who like this

0 Show 1
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 iwaldrop · Feb 12, 2013 at 04:39 AM 1
Share

Hi. I got way into playing with your script. :)

Needless to say, there are quite a few things that still need to be worked out, like engine load for instance. But that's a pretty decent approximation of speed. You can change the gear rations and the tire dimensions to effect the simulation. Speed is in MPH, but the conversion to m/s is there; you just have to uncomment it. You're going to want to do that if you plan on simming anything in Unity, because Unity's world units are in meters.

 using UnityEngine;
 using System.Collections;
  
 public class MoveCar : MonoBehaviour
 {
     public float redlineRPM = 6150;
     public float maxRPM = 7000;
     private float maxDamage = 100;
     public float finalDrive;
     public float[] gearRatios;
     public TireDimensions tireDimensions;
     //public bool hasAutoTransmission = true;
     
     [HideInInspector] 
     public float damagePerc; //??
     
     private float tireCircumference;
     private float currentRPM;
     private float currentDamage;
     private float currentSpeed;
     private int currentGear;
     private bool reverse;
     
     void Awake()
     {
         tireCircumference = tireDimensions.Circumference / 12;
     }
  
     void Update()
     {
         bool changeUp = Input.GetKeyDown(KeyCode.Q);
         bool changeDown = Input.GetKeyDown(KeyCode.A);
         if (changeUp || changeDown)
             ChangeGear(changeUp);
         
         reverse = Input.GetKey(KeyCode.DownArrow);
         if(Input.GetKey(KeyCode.UpArrow))
         {
             if(currentRPM < maxRPM)
                   currentRPM += 10;
         }
         else
             currentRPM -= reverse ? 15 : 5;
         
         if (currentRPM < 0) currentRPM = 0;            // current RPM should never be 0 while the engine is running.
         
         // mph = (rpm * tireCircumfrence in feet) / (gear * final * 88) -- ref: http://www.type2.com/library/misc/calcspd.htm
         currentSpeed = ((currentRPM * tireCircumference) / (gearRatios[currentGear] * finalDrive * 88));// * 0.44704f;    // 1 mph = 0.44704 m/s
  
 #if UNITY_EDITOR
         Debug.Log(string.Format("Gear Ratio: {0}, Speed: {1}, RPM: {2}", gearRatios[currentGear], currentSpeed, currentRPM));
 #endif
         
         // move the object
         transform.Translate(Vector3.forward * Time.deltaTime);
     }
     
     void ChangeGear(bool changeUp)
     {
         if ((changeUp && currentGear < gearRatios.Length-1) || (!changeUp && currentGear > 0))
         {
             currentGear += (changeUp ? 1 : -1);
             //currentRPM *= (changeUp ? 0.5f : 2);
         }
     }
     
     [System.Serializable]
     public class TireDimensions
     {
         public float tireWidth = 215;
         public float sidewallHeightRatio = 45;
         public float wheelDiameter = 16;

     public float Circumference
             {
                     get { return (((tireWidth / 25.4f * sidewallHeightRatio/100 * 2) + wheelDiameter) * Mathf.PI); }
             }
     }
 }

0 Replies

· Add your reply
  • Sort: 

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

transform.rotation problem 1 Answer

Freezing Transforms 2 Answers

How to change position after some reference. 0 Answers

Detect shake motion on the mobile 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