19.01.2015, 17:20
Just make a timer which checks if the player is inside a vehicle as driver, with the engine turned on, and make it run every second.
You could make it run every minute, but players could abuse it by getting out of the vehicle every 55 seconds (when they time it right) and consume 0 fuel.
If he is, multiply your required fuel consumption with the speed, then divide by 100.
That will make your vehicle consume more fuel at higher speeds.
Say you want vehicles to consume 5l per 100km when they drive 100kph.
Your fuel consumption would be:
5 litres per hour => 5/3600 = 0.00138888 litres per second
When you drive 100kph, your vehicle consumes 0.0013888888 litres per second.
When it drives 150 kph, that would be 0.00138888 * 150 / 100 = 0.0020833 litres per second.
Very easy to do, I have a similar system, but a little bit expanded, as every vehicle model has it's own fuel-tank size and consumption values, so planes consume alot more fuel compared to bikes and cars.
Be careful, as someone standing still would have a speed of 0, and he will consume 0 fuel per second.
Vehicles in real-life always consume fuel when the engine is turned on, even when standing still, so I figured vehicles should consume fuel as if they were driving 5kph when they drive slower than that.
If you detect a speed below 5kph, then use 5kph in your calculation, if you want it, or adjust the numbers to your own wishes.
You could also detect if the vehicle has a negative fuel-readout, then turn off the engine.
That's very nice when using planes and helicopters
You could make it run every minute, but players could abuse it by getting out of the vehicle every 55 seconds (when they time it right) and consume 0 fuel.
If he is, multiply your required fuel consumption with the speed, then divide by 100.
That will make your vehicle consume more fuel at higher speeds.
Say you want vehicles to consume 5l per 100km when they drive 100kph.
Your fuel consumption would be:
5 litres per hour => 5/3600 = 0.00138888 litres per second
When you drive 100kph, your vehicle consumes 0.0013888888 litres per second.
When it drives 150 kph, that would be 0.00138888 * 150 / 100 = 0.0020833 litres per second.
Very easy to do, I have a similar system, but a little bit expanded, as every vehicle model has it's own fuel-tank size and consumption values, so planes consume alot more fuel compared to bikes and cars.
Be careful, as someone standing still would have a speed of 0, and he will consume 0 fuel per second.
Vehicles in real-life always consume fuel when the engine is turned on, even when standing still, so I figured vehicles should consume fuel as if they were driving 5kph when they drive slower than that.
If you detect a speed below 5kph, then use 5kph in your calculation, if you want it, or adjust the numbers to your own wishes.
You could also detect if the vehicle has a negative fuel-readout, then turn off the engine.
That's very nice when using planes and helicopters