I just picked up unity a few weeks ago and started working on a platformer for my first game. For jumping I am adding a impulse force on key down. Since it was on key down I did this in update so the event wouldn’t be missed and the physics had no issues.
I moved on to wall jumping today and where the player jumps off of walls like you could in Mario or many other platformers. Since it was on keydown I did this just like I did jumping but I started noticing inconsistent behavior with the physics. Sometimes the force was not applied, sometimes it stopped midway and other times it worked.
Moving the jump machinics to fixedupdate fixes the physics issue but then keydown events are not always caught in fixed update so I will have to listen to keydown in update and run the physics in fixed update.
Assuming I will need to do this for other actions what is the best way to track the state of the keys and pass it to fixed update without creating a ton of variables? or is there a better way to do this?
Basically, void Update would probably be the best method to catch Input from user. But you should always try to use some variables in order to prevent it from bugging! Hope it was the right answer to your question!
How about handling the input in update (so nothing is missed) and setting a flag that a jump is triggered, then in fixedupdate read this flag and if it is set, do the jump and clear the flag?
You are correct. Input checking in Unity is not overly pleasant. Not being able to configure input to catch it directly in FixedUpdate is one of the issues that crops up. The other is that axes bindings can not be changed at run time.
Why are you not using the different key inputs? You dont need “isDown”. You can just say if(Input.GetKey(KeyCode.E)) and it will check if you are holding down E button. If you say if(Input.GetKeyDown(…)) then it will check if you just click the button. After using Input.GetKey you can use Input.GetKeyUp. Then by these input methods, you can assign variables inside of them. Basically, 8 buttons = 8 bools mostly - depending on how much you need them.
For most situations that will do but in some cases it wont. For example in Mario the fire button only fires on key down but can be used to run if held and he wont fire again until you repress the button.
Sure I could listen to only the key event but then I have to track if it was the first instance of being true. To me it seems easier and more readable to listen to the two different events of keyDown and key.
it would be simpler to do isDown = Input.GetKey(KeyCode.E) for the use case you are suggesting. your code makes it impossiable to do something soon as the key is lifted.
I am asking about cases where you want to do a different actions for each of the states KeyDown, Key and KeyUp. I am currently doing this like so
You can use as much as you want in one single key press, and it’s pretty usefull to just have one variable that you assign to do some stuff, while the input itself can take care of some other stuff. Obviosuly it’s just another method, but I’d suggest you less variables. What you’re doing seems like duplicating stuff to me.
@Sykoo The whole point of the variables is to capture the input inside of Update, then use it inside of FixedUpdate. As these don’t run at the same rate you can’t simply set the bool to true of false in Update, this would be exactly the same as checking for input in FixedUpdate. At best you need to set a bool true in Update and false in FixedUpdate. This doesn’t allow for sophisticated input detection, and ends up with complicated if statements.
@yamiko Did you ever consider doing something event based, like the old OnGUI system? Its more complex to code, but could potentially make your code look better and tidy up all of those nasty if statements. Create a struct to store input, build a queue. Enqueue each input during Update and Dequeue it during FixedUpdate?
Not sure how this approach would run in terms of efficiency. But it would eliminate the need to write tonnes of variables to transfer each input across to FixedUpdate.
Its also worth checking which inputs actually need to go to FixedUpdate, and which can be handled in Update. In general things like physics based jumping and movement should be in FixedUpdate. But shooting, dialogue, menus, regular movement and any other non physics behaviour doesn’t actually need its input translated. I’d be surprised if all 12+ of your buttons needed to be read by FixedUpdate.