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