Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 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
Add comment · 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 $$anonymous$$PH, 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 sim$$anonymous$$g anything in Unity, because Unity's world units are in meters.

 using UnityEngine;
 using System.Collections;
  
 public class $$anonymous$$oveCar : $$anonymous$$onoBehaviour
 {
     public float redlineRP$$anonymous$$ = 6150;
     public float maxRP$$anonymous$$ = 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 currentRP$$anonymous$$;
     private float currentDamage;
     private float currentSpeed;
     private int currentGear;
     private bool reverse;
     
     void Awake()
     {
         tireCircumference = tireDimensions.Circumference / 12;
     }
  
     void Update()
     {
         bool changeUp = Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Q);
         bool changeDown = Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A);
         if (changeUp || changeDown)
             ChangeGear(changeUp);
         
         reverse = Input.Get$$anonymous$$ey($$anonymous$$eyCode.DownArrow);
         if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.UpArrow))
         {
             if(currentRP$$anonymous$$ < maxRP$$anonymous$$)
                   currentRP$$anonymous$$ += 10;
         }
         else
             currentRP$$anonymous$$ -= reverse ? 15 : 5;
         
         if (currentRP$$anonymous$$ < 0) currentRP$$anonymous$$ = 0;            // current RP$$anonymous$$ 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 = ((currentRP$$anonymous$$ * 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}, RP$$anonymous$$: {2}", gearRatios[currentGear], currentSpeed, currentRP$$anonymous$$));
 #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);
             //currentRP$$anonymous$$ *= (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) * $$anonymous$$athf.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.

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

Continuous detection of object using ray cast 2 Answers

Car rolls over when turning in real-time 1 Answer

Vector3 Returning Infinity Error 2 Answers

How do I create a rubber band effect on a camera forward movement? 1 Answer


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