- Home /
Well coded or just silly?
Okay, this is a pretty low-priority question, but for learning's sake, I wanted to know. The code below works fine for what I wanted to do, but frankly with all the conditions going every which way I have no idea if this was best practice. Is there an easier or more efficient way to do the same thing?
//Pressing the button when ready makes the player jump,
//and disables the ability to jump again.
if(jumpEnabled && Input.GetButtonDown("Jump")){
_myMovementScript.jumping = true;
jumpEnabled = false;
}
//The same button pressed again can start flying behavior.
else if (flightEnabled && Input.GetButton("Jump")){
_myMovementScript.flying = true;
}
//If the player releases the button they will be neither flying nor jumping.
//Flight is only enabled after releasing the button the first time.
else if(!Input.GetButton("Jump") && !_myMovementScript.onGround)
{
flightEnabled = true;
_myMovementScript.flying = false;
_myMovementScript.jumping = false;
}
//The player can only jump again from the ground.
//It enables only when the button is released to avoid holding the button and jumping continuously.
else if(!Input.GetButton("Jump") && _myMovementScript.onGround){
_myMovementScript.jumping = false;
_myMovementScript.flying = false;
jumpEnabled = true;
flightEnabled = false;
}
//Handles the player still holding the button when neither action is enabled.
else
{
_myMovementScript.jumping = false;
_myMovementScript.flying = false;
}
There's a couple places where your comments say 'only when button is released' but your condition is just GetButton ins$$anonymous$$d of GetButtonUp. Otherwise, the code that works best is code you can maintain (and works of course). I'd say leave it and move on, you don't want to change it to 'advanced & abstracted' code you get from someone else that you can't maintain or modify.
When learning, the best code in my opinion is code you write yourself. To many times to people copy and paste code and then have no idea how it works.
If you have written you own code and it works, regardless of best practice of efficiency shows that you understand the concept and a loose flow of what your trying to achieve looks like.
After you fully understand what you have written can you come back and try and push your boundaries of knowledge and think of better ways to re-write something.
You are creating a statemachine using if statements, which is fine but does tend to spiral out of control. If you think about states then you can make this kind of thing more efficient and readable.
States would require you branching earlier on the current mode of the character and then testing the input valid in that state.
For instance you could use a switch based on the current mode (like idle, walking, running, jumping, attacking, falling or dying). Within each of those modes you would test your input and make any other specific changes. more advanced techniques would include using delegates or inheritance to have different code run in each state and you might consider the code you run as states are exited and entered (transitions).