It works, but I need to use a lot of keys… isnt there a better way to do this? I dont know if its ok to have all this input checks in the player´s update function… what do you think?
input code is something that should be run once per frame so there is no way out. the best way is to have an input component that checks all input and call other methods and components based on the key pressed. if you have different input keys that should be enabled or disabled in certain times in your game, then you can use bool variables for them or disable the input component for the group of keys that you don’t need.
for example if you have a player that sometimes in the game can use A,S,D to fight and sometimes can not you can have
var fighting : boolean;
function Update ()
{
if (fighting)
{//write input code for A,S,D }
}
the only special time is when you use physics. for example use the mouse to controll the physx based ball, in these situations some people write their input code in FixedUpdate. keep in mind that input code will update only once per frame so if you have 3 FixedUpdates in between two frames then the values of inputs will not change in those 5 calls and queries like GetKey. for example if you pressed 1 in last frame then in all those five fixedUpdates GetKey(“1”) will return true. so there is no difference to use Update to check input and set parameters for codes in FixedUpdate and checking input in FixedUpdate.
if you want to make the game performance higher, don’t worry of this small piece of if statements just check them in one place and use the data in all gameObjects that need it. if you have 3 components (scripts) that need to know if 1 is pressed or not, check it in one place and call all 3 components using GetComponent. remember to use GetComponent once in Start/Awake to cache the component in a variable and use it in later times.
there is not any other place to check for input because it should be checked always to get interactivity to your application/game. you can not check in two times a second in a coroutine or at collision times so the only event to do it is Update/LateUpdate (for camera as an example). the only improvement that you can offer to your code is using input axis and Input.GetAxis methods to make the user able to change the keys for certain action at runtime or you yourself to change them in editor in development time. also you can expose string or KeyCode type variables and fill them yourself.
var key1 : KeyCode;
var key2 : KeyCode;
function Update ()
{
if (Input.GetKeyDown(key1))
{
//do something
}
if (Input.GetKeyDown(key2))
{
//do something
}
}
then set the key1 and key2 in inspector to 1 and 5 or any other keys. in this way you don’t need to change the code to change the keys. also use function calls inside if statements again to don’t have to change input code to change the behaviour of your objects. try to write your code input independent. for example write a jump function that takes a number as height and when user pressed space call it with a good height value but don’t check input in jump method itself, in this way you can easily port code between platforms and port inputs in only few scripts.