Game programming - Unity 5

My first game in Unity part6 – Fuel gauge and consumption

After having done a speedometer, I wanted to make a fuel gauge even though I don’t plan to make the gameplay centered on that (unlike most of nowadays games of that kind where the vehicle runs out of gas [way too] fast). But it adds some realism to it.
Like the speed needle, I did almost the exact same thing, with obviously more data because consuming fuel requires some calculation. First, let’s add in the GameManager script the stuff that is going to be used:

Now, I create a private void the FixedUpdate() in which I defined some values and where I added the condition whether the car runs out of petrol or not. That condition is, as written in green, linked to the script of the car.

So let’s see the car’s script:

In the previous post, I introduced the
//Speed display in KPH conditions (or else it’s never 0)
so I won’t be talking about it.

Fuel consumption
I made a condition for the fuel consumption, so that it doesn’t empty itself when the engine is not being used. Technically, once a vehicle’s engine is on, it will consume a small amount of fuel (logical, right?), and much more once it’s in use and depending on how much power is needed. I wanted to keep it simple, and it would only be consuming gas when:
1) The up or down arrow key is pressed (=we’re pressing the pedal)
2) The front wheels are in contact with the ground (so it doesn’t use gas when the car is in the air, which would be nonsense to me)
3) The tank is not empty
Which gives us:
if (movement != 0f && GroundColScript.GroundCollision && !FuelScript.EmptyTank)
distanceTravelledMotor += Vector3.Distance(transform.position, lastPosition) / 1000f;
The distanceTravelledMotor is used in 2 ways. Here for the condition to say WHEN the fuel is being used, and in the GameMangager script to make distanceTravelledMotor2. The latter is used for the actual consumption, cf:
distanceTravelledMotor2 = Truck.distanceTravelledMotor*80;
fuelConsumption = distanceTravelledMotor2;
gasTank = fullTank – fuelConsumption;
You can see that, based on (my) common sense, the capacity of the gas tank is calculated according to its full capacity (I chose 30) minus how much has been consumed (between 0 and 30, obviously). Once the tank is empty, then the car cannot be driven anymore, and we get the //No more gas part.

When running out of gas
So here, the thing happens in 2 parts. The vehicle runs out of gas, and it is likely to be moving. First, the motors are deactivated (so the player cannot drive the car anymore) and drag is turned on (which slows down the car).
if (FuelScript.EmptyTank)
rb.drag = dragForce;
rearWheel.useMotor = false;
frontWheel.useMotor = false;

Because the car takes too long to completely stop, I added one more condition that says that when the velocity is between -0.2 and 0.2, the front motor will be activated with a speed to 0, which immobilizes the vehicle. That simple!
if (rb.velocity.x < 0.2 && rb.velocity.x > -0.2)
frontWheel.useMotor = true;
JointMotor2D motor = new JointMotor2D { motorSpeed = 0f, maxMotorTorque = frontWheel.motor.maxMotorTorque };
frontWheel.motor = motor;
speed = 0;

I’m not sure if it looks messy or not, however it is working and serves its purpose well.
EXCEPT  there is something I haven’t taken care of: braking is considered as driving, hence it consumes gas in a normal way. I’m still considering if I should let it be part of the gameplay.

If you’ve got any questions, throw them at me!

Leave a Reply