Multiple input key press actions

Hey!

I’ve been developing a 2D game and I need my player to shoot vertically/diagonally.

When player is looking up, the weapon’s location will be switched to different socket and now when shoot is pressed, it should shoot vertically.

Or when player is holding “Look up” and “Look left / look right” button and shooting, the player should be able to shoot diagonally. I’ve thought to approach this problem with booleans, but it seems to get really complicated with booleans when I have multiple similar actions (there will be alot boolean type variables).

Do you have any suggestions? Any patterns perhaps?

With regards.

I had a similar requirement for a different feature. The way I solved it was to:

  • Bind Inputs in Character to know when Forward, Backward, Left and Right keys are down
  • Create a calculator that determines the current movement direction
  • Create an instance of this direction calculator on my Character
  • Notify calculator when Character Movement inputs occur
  • Whenever Character needs to know what keys are down it calls the Movement Direction calculator

It’s quite cumbersome to calculate this but I “over-formalised” it into an InputDirectionCalculator as you can see here: Bitbucket

Sure no worries.

Yes, as long as you keep track of the directional keys’ down states it should be perfectly fine to query their combined states at any given time (in effect what I did in the function UpdateMovementDirection()).

As for the bullet direction, yes, if you also implement the direction vector part then you can easily use that for setting the bullet’s velocity - just be careful to normalise the vector first (I had a specific movement working off this where I did not want the direction vector to be normalised). E.g. FVector(1.0f, 1.0f, 0.0f) is not normalised, so if you want to consistently set the bullet velocity you’ll need to do something like:

 FVector aimDir = FVector(1.0f, 1.0f, 0.0f);
 aimDir.Normalize();
 float requiredBulletSpeed = 500.f;
 
 FVector bulletVelocity = requiredBulletSpeed * aimDir;

As for the sockets, I’m not 100% following what your setup is like but have you had a look at Aim Offsets? Seems it might be what you’re looking for.

Thanks alot for your help!

I tried to create something similar without the code being in a different class (thanks for “over-formalising” it!!). It seems the easiest approach. So when I’d call shoot function I just setup a switch there and based on the movementdirection also set the bullet’s velocity?

About the sockets tho… Right now I’m switching between socket locations whenever I look up with my character. But as “looking up” is axis based input event, it fires all the time and constantly sets location to either one of those socket locations. But as I see it now, there should be “look-up” animation separately and the sockets could be just bound to those animations so I shouldn’t switch between socket locations repeatedly. But when animation state changes, also the weapon location changes.

Thanks for your help once again!

With regards!

Thanks once again!

As I’m developing the game in 2D, so the sockets are sprite-based (points on 2D texture that have transform). I’m gonna give your suggestion a try now :slight_smile:

With regards.

Hello again!
I’m sorry I didn’t ask you about this earlier, but I wanted to ask you if you permit me to use this code snippet in my thesis (I have modified it a little bit). I don’t want to run into problems where I feel I’ve used something that’s based on someone else’s creation. It would be a big deal for me :slight_smile:

Oh absolutely! No need to even give credit/citation. Good luck!

Thanks! I appreciate it very much! :slight_smile: