- Home /
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);
}
}
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); }
}
}
}