Looking for a tutorial re: handling inputs/camera/animations

A month or so, I wrote a 3rd person character controller from scratch, basically as a learning experience, but I’m also working towards starting a game using it eventually. I got it written and working fine using a capsule for a character, but there were some complications when I started to extend it.

First, handling camera movement was a little glitchy when the cursor was over a UI element, or when I had a menu displayed.

And now, I’m attempting to replace the capsule with a Mixamo model and hook up the animations to play at the proper times and I realize that my controller is a mess. Recently I read in a thread here that said it’s good advice to separate all your inputs from the player movement and camera movement code (which I didn’t do), so that’s complicated things too.

So, I’m re-writing it from scratch, attempting to make it more modular, and trying to work in the animations as I go. I’m also hoping to use a game state manager to control everything once I’m done, so I want to set it up to work with that eventually. Unfortunately, I’m just not getting my head around how to do it all and make everything work together well.

I think part of my problem is not knowing when to use Update() vs. FixedUpdate(). I tried grabbing all the input in Update, and I had my movement code in FixedUpdate in another script, but things didn’t seem to work well that way - it seemed like keypresses got missed and it just didn’t feel solid.

So, TLDR; does anyone know of a good tutorial or blog post that would help set up a modular input/camera control system, linked with Mixamo animations?

I don’t have a tutorial, but I might have insight into your missed keypresses.

If you are using Input.GetMouseButtonDown() or Input.GetKeyDown(), those will only return true reliably in Update().

FixedUpdate() and Update are NOT called at the same rate. That’s the entire point of them.

If you are simply assigning your “down” input to a boolean, then “using” that boolean in FixedUpdate(), you might miss it under these circumstances:

Update() happens → detected the condition as true, set boolean
Update() happens → no keypress so you clear your boolean
FixedUpdate() happens - boolean clear - no event apparently happened

What you want to do is set up some kind of producer and consumer pattern for these events, such that when an event is produced in Update(), it is later consumed only by FixedUpdate(), not overwritten in the subsequent calls to Update().

You can go crazy and implement some kind of queue or something, but I wouldn’t bother with that much code.

I would just make a simple integer that counts how many of a given action occur:

 int whateverKeyCount;

In your Update() you would have:

if (Input.GetKeyDown(whateverKeyCodeYouCareToCheck))
{
  whateverKeyCount++;  // produce the event
}

In your FixedUpdate() you could have either:

while( whateverKeyCount > 0)
{
  // TODO: Process the action you want here

  whateverKeyCount--;  // consume the event
}

Or if you want to process each event on separate calls to FixedUpdate(), replace the “while” above with a simple “if”

Obviously the same goes for Input.GetMouseButtonDown() style processing.

That will help - yes, it was a boolean I was having the problems with. I was storing the result of the keydown in a variable, but I hadn’t thought of it being cleared before FixedUpdate got to it. That makes sense.

Thank you! :slight_smile:

1 Like