how to correct my script to stop the jetpack working when out of fuel

trying to sort out movement in a game im making with friends and none of us can figure this out, we’ve got the jetpack working fine and have it to reduce my fuel value, however when trying to impose a restriction on the jetpack to not work with fuel at 0, however anything weve tried just gets different errors each time, any help as to how to make this work would be appreciated.
script:

using UnityEngine;

using System.Collections;

public class Jetpack : MonoBehaviour

{

public float jetpackForce = 75.0f;

public float acceleration = 1.0f;

void FixedUpdate () 
{
	bool jetpackActive = Input.GetButton("Jump");
	float fuel = 100.0f;

	if (fuel) -= 0
	{
		bool jetpackFueled = true
	}

	if (jetpackActive) (jetpackFueled)
	{
	        rigidbody2D.AddForce(new Vector2(0, jetpackForce));
		    fuel = Mathf.Clamp(fuel - acceleration * Time.deltaTime, 0, 100);
	}
}

}

errors:

(15,28): error CS1525: Unexpected symbol ‘-=’

(15,17): Possible mistaken empty statement

(26,1) error CS8025: parsing error

2 Answers

2

Two simple programming mistakes,

First One,

Error:

if (fuel) -= 0
{
       bool jetpackFueled = true;
}

Lots of things are wrong here. First of all, that is not how you write an if loop. Secondly, your variable jetpackFueled should be defined outside the scope of the if loop. The current implementation defines the variable inside the if loop, and thus destroys it as soon as the loop is exited.

Solution:

bool jetpackFueld = false; 

if(fuel <= 0)
{
       jetpackFueled = true;
}

Second One,

Error :

 if (jetpackActive) (jetpackFueled)
{
        // Your Code Here
}

Again, thats not how you write an if loop,

Solution:

if ((jetpackActive) && (jetpackFueled))
{
       //Your Code Here
}

I strongly suggest you to study the basics of programming, before getting started with scripting in Unity.

thanks for the reply, yea its been a while since ive scripted anything and it was just my bad memory then, and i havent tried using two conditions so thanks for the info on that.

@Niketas Yeah, study C# basics first..

Hey, try this out and see if it works

 void FixedUpdate () 
 {
     bool jetpackActive = Input.GetButton("Jump");
     float fuel = 100.0f;
5.
     if (fuel <= 0)
     {
         bool jetpackFueled = true
     }
10.
     if (jetpackActive && !jetpackFueled)
     {
             rigidbody2D.AddForce(new Vector2(0, jetpackForce));
             fuel = Mathf.Clamp(fuel - acceleration * Time.deltaTime, 0, 100);
15. }
 }

I don’t know if this Mathf.Clamp will work as you expect, i suggest you try different values if it doesn’t work as you want.

As a side note, the problem with the code were the conditional operations. When you want to something happens when a determined valued is reached you have to do something like this:

if (fuel <=0){

That way you say, when the variable fuel is less or equals to 0 then do what it is in the bracket. Similar to (jetpackActive && !jetpackFueled) condition, where you want to only be able to use the jetpack when the variable jetpackActive is true (if the player is pressing the button jump) and the variable jetpackFueled is true. The ! operator reverses what the booleans states, so you can only active the jetpack when the jetpackActive is true AND (&&) the jetpackFueled is false.

I suggest you study a little of operational conditions in C# so you can undestand better what i tried to explain.