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:
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.
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.
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.
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@Niketas Yeah, study C# basics first..
– imilanspinka