Hi folks,
I’m doing a 2D game where the player can move left or right.
I want to be able to check easily if the player is moving or not.
To do that I’m checking the keyboard inputs.
The first way I thought of was to check at Update() and to store the result in a boolean :
bool isMoving;
void Update() {
isMoving = Input.GetKey(Keycode.LeftArrow) || Input.GetKey(Keycode.RightArrow);
}
void Check() {
if (isMoving)
// do something
}
But I don’t need to check each frame, I need it on a per-demand basis.
So I wondered if it would be better (and cheaper in memory usage) to implement that kind of boolean method :
bool IsMoving() {
bool b = Input.GetKey(Keycode.LeftArrow) || Input.GetKey(Keycode.RightArrow);
return b;
}
void Check() {
if (IsMoving())
// do something
}
The answer may seem obvious but I wonder if there would be issues with the second method, or significant performance difference ?
I have to precise that I also check the inputs individually (to move the player, obviously).
Thanx in advance for any advice. Have a nice day.