Making a drag racing game and am a mechanic by trade so these formulas work to get real world data, however i cannot get them to work back to back in my code.
function FixedUpdate(){
horsePower = engineTorqueRPM/5252;
engineTorque = horsePower5252/RPM;
I want to run these just like this in FixedUpdate but only one changes when running program. I set default values for these and will mod them as you purchase add on parts and what not however it is not going to be very accurate unless i can get these to work because in a real engine torque decreases as horsepower increases but if you run this without the other one getting up to second info then torque just falls or hp jumps and is not a very good torque curve. Thank you For any help if you have any.
The first one happens first. If you change HP elsewhere, it won’t matter, because in fixedupdate it will be set back.
You need two functions in that script:
function SetHorsePower( amnt : float ) {
horsePower = amnt;
engineTorque = horsePower*5252.0 / RPM;
}
function SetEngineTorque( amnt : float ) {
engineTorque = amnt
horsePower = engineTorque * RPM / 5252.0;
}
Use those to change the torque/HP instead of altering them directly, and don’t worry about updating the values in FixedUpdate because they’ll always be up to date.
Thank You so much this works beautifuly!!! if thats a word…
It is (beautifully), and you’re welcome. ![]()
Ok now i want to get more specialized in when and how to make a certain combo of heads, exhaust, ect…more low end torque, high end torque, and so forth. The way I think best to do it is to make a .ini file or something like it that looks something like this.
RPM Torque Horsepower
1000 160 60
1500 225 75
2000 300 125
is there a way to utilize something like this with Unity? And here i would actually modify variables to enhance these numbers due to cams heads exhaust and other mods.
Well you have two options. Either you define the relationships as mathematic functions or you can use a lookup table (which could be loaded from a file if you like).
I think a lookup table makes the most sense as the relationships aren’t very easy to represent as mathematic functions. Here is something I found doing a google image search for “torque graph”

You can find graphs like this for many models of cars… Of course the actual speed will depend on what gear you are in (as RPMs here indicates the engine crankshaft revolutions per minute)
You could represent the relationships between torque:rpm and hp:rpm as two separate arrays… then you lookup the value by using the RPM as the index
torque = torques[rpm];
hp = hps[rpm];
or
rpmStep = 100.0f //we indexed rpm values by 100s ie: 100rpm,200rpm,300rpm, etc.
torque = torques[Mathf.RoundToInt(rpm/rpmStep)];
hp = hps[Mathf.RoundToInt(rpm/rpmStep)];
This may be a stupid question but how do i use the graph? I went to the scripting reference and searched for lookup table and couldnt find one. So i could make or find charts like this one for different settings and call from the chart the info based on the rpm? I have programed before but Java is new to me I used Angelscript for the most part and alot of nintendo ds language so i appoligize if these are questions i can answer myself show me where and I’ll leave yo’ll alone. I use the scripting, referance, and manual all the time and i bought the book for the essentials but doesnt cover much in the coding department. Thanks again.
Can anyone help me to use this? I can’t find it anywhere!!
You actually have to “hardcode” the lookup table. If you look at that image x-axis you see vertical lines every 500 rpm.
so we could say
var rpmStep:float = 500f;
var powerLUT:Array = new Array(8) {0f,10f,100f,160f … ,315f}; //get these from the power values on the chart
var torqueLUT:Array = new Array(8) {0f,150f,225f,…}; //get these from the torque values on the chart
…
currentRPM = 1200;
torque = torqueLUT[Mathf.RoundToInt(currentRPM /rpmStep)];
power = powerLUT[Mathf.RoundToInt(currentRPM /rpmStep)];
you would probably actually want to interpolate the torque and power.
var t:int = Mathf.FloorToInt(currentRPM/rpmStep);
var tf:float = currentRPM - t;
torque = Mathf.Lerp(torqueLUT[t],torqueLUT[t],tf);
But you would have to first see if your RPM is higher than the upperbound of your lookup tables. I hope this gets you started, take some time to look at what’s happening mathematically here. There might be errors, I usually code in C# so I dunno if instantiated the arrays the right way.
The scripting reference only deals with Unity specific methods and classes, it doesn’t actually tell you how to program anything. For that you’ll need to Google around and do some reading. If you search Lookup tables you will find lots of instances of how they are used. Mainly they are used for relational values (like RPM and torque, for example) which are directly related but it would be cumbersome and “expensive” in terms of CPU usage to actually calculate the values on the fly.
I’m sorry for digging this up but I’m also struggling with the same problem in implementing the formula into my game. Since horse power and torque depend on each other and are unknown, how do you get it to work? Would you mind giving a snippet of the script we you implement those 2 functions?
I’m also curious about how to determine the engine acceleration, so I know how fast the RPM goes up.