How to make a jetpack with fuel?

Hi, I want to create a jetpack with fuel. I already got the script to fly with the jetpack.

var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
var maxFuel : float = 100;
var fuel : float = 10;

private var moveDirection : Vector3 = Vector3.zero;
function Update() {
    var controller : CharacterController = GetComponent(CharacterController);
    if (controller) {
        moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
                                Input.GetAxis("Vertical"));
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection *= speed;
        
        if (Input.GetButton ("Jump")) {
           moveDirection.y = jumpSpeed;
        }
    }
    moveDirection.y -= gravity * Time.deltaTime;
    
    controller.Move(moveDirection * Time.deltaTime);
}
  1. I want to create a variable with fuel. The fuel should reduce, when i press the “Jump” button. (Maximum Fuel should be 100, and every time the button Jump is pressed reduce 10 or like for every second the button is pressed the fuel reduce 10)

  2. Also i want to fill the fuel for every second the player isGrounded. (+10 every second)

  3. When the fuel count is 0, the jetpack shouldn’t fly anymore. I want the var fuel dynamic, so that the current fuel should be always printed in the console.

I’m pretty new to unity and i hope someone could help me to fix the problem.

#1)
I would rename fuel to dFuel just so the name makes more sense (Rate of change of fuel). You could make another variable, maybe currentFuel or whatever name you feel is right. Define the function Start() and inside set currentFuel to maxFuel. This will be called by Unity just before the scene starts.

To get the fuel to decrease at 10 units per second change the if statement to something like this:

 if (Input.GetButton ("Jump") && currentFuel > 0.0) {
        moveDirection.y = jumpSpeed;
        currentFuel = Mathf.Clamp(currentFuel - 
                  (dFuel * Time.deltaTime), 0.0, maxFuel);
    
}

What this does is if we have fuel, move the character up and consume some fuel. The call to clamp just makes sure that currentFuel is never negative as this wouldn’t make any sense and may cause bugs.

#2)
The simple answer is that there is no best way for #2. You could look at using colliders and Unity will tell you when you’ve collided with another object. This has the draw back that you have to make sure that your collider information is always correct in the editor for it to work. I would probably use this method as it would be less bug prone in code and tracking editor information is relatively easy in Unity. You’d have to look more into this yourself as I’m not quite sure how to use the colliders.

Another option is to just keep a velocity or position of the last frame and compare it to the velocity of this frame. If the change in the two velocities is under some threshold then you consider it as being on the ground. The issue with this solution is that on the arc the change in velocity will meet this threshold in at least two places, at the top of the flight and on the ground. That’s only assuming that the player consumes all their fuel before stopping as well.

#3)
The code for #1 should also fix this scenario. To make a variable print to the console just use the print(currentFuel) function and place it in the update function.

This is simple my friend! I will try to explain it the best I can :slight_smile:

The line :

if (Input.GetButton ("Jump")) 
{
   moveDirection.y = jumpSpeed;
}

States that if the user presses a button known as Jump (Which is set inside Unity’s options and is known as space) then move the player up using the jumpSpeed amount.

You can change this too: Not Tested

if (Input.GetButton ("Jump") && fuel > 0) //If the button is pressed and the player has fuel
{
   moveDirection.y = jumpSpeed;
   fuel -= 10 * Time.deltaTime; 
   print(fuel);
``//Take the fuel away every second by 10 when the jump     button is pressed
    }
else if (fuel < maxFuel && isGrounded == true) //If the players fuel is less than the max fuel and the player is grounded
{
    fuel += 10 * Time.deltaTime;
    print(fuel);
//Increase the players fuel every second
}

Hope this helps!