- Home /
Question by
$$anonymous$$ · Mar 23, 2020 at 09:47 PM ·
carcar gamecar physicsrealisticshift
Shift gear with own RPM? Help please!
Hello! I comment:
I'm making a script of a car, and I need to make each gear change have its own RPM
that is, the 2nd gear shift goes up to the 3rd gear shift at 6000 rpm, and lowers gear at 3000 rpm. (example) and so on with everyone else.
someone could help me? thanks!
script:
using UnityEngine;
using System.Collections;
public class CarPlayer : MonoBehaviour {
public WheelCollider FrontLeftWheel;
public WheelCollider FrontRightWheel;
public float[] GearRatio;
int CurrentGear = 0;
public float EngineTorque = 600.0f;
public float UpGearRPM = 3000.0f;
public float DownGearRPM = 1000.0f;
public float EngineRPM = 0.0f;
public Vector3 CenterOfMass;
void Update (){
rigidbody.centerOfMass = CenterOfMass;
rigidbody.drag = rigidbody.velocity.magnitude / 250;
EngineRPM = (FrontLeftWheel.rpm + FrontRightWheel.rpm)/2 * GearRatio[CurrentGear];
ShiftGears();
FrontLeftWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical");
FrontRightWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical");
FrontLeftWheel.steerAngle = 10 * Input.GetAxis("Horizontal");
FrontRightWheel.steerAngle = 10 * Input.GetAxis("Horizontal");
}
void ShiftGears (){
int AppropriateGear = CurrentGear;
if ( EngineRPM >= UpGearRPM ) {
for ( int i= 0; i < GearRatio.Length; i ++ ) {
if ( FrontLeftWheel.rpm * GearRatio[i] < UpGearRPM ) {
AppropriateGear = i;
break;
}
}
CurrentGear = AppropriateGear;
}
if ( EngineRPM <= DownGearRPM ) {
AppropriateGear = CurrentGear;
for ( int j= GearRatio.Length-1; j >= 0; j -- ) {
if ( FrontLeftWheel.rpm * GearRatio[j] > DownGearRPM ) {
AppropriateGear = j;
break;
}
}
CurrentGear = AppropriateGear;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Better RPM?? 0 Answers
transmission clutch help! 1 Answer
Car shifting effect? Help please! 1 Answer
How to make traction control for car? 1 Answer
how to rotate car to camera direction using wheel colliders. 1 Answer