2 Players 1 Keyboard

Hello everyone, I am developing a small 2D game where you have two players in an arena that have a 1v1 battle. I was wondering how to make it so I can spawn both players into the and have player one use WASD to move and maybe E to shoot and player two to use the arrow keys and maybe L to shoot (on the same keyboard). I’m pretty new to UE4 so I don’t know how to implement this.

All the animations are set using an enum that sets specific states after each action, which then plays a certain animation for this state.

If it helps this is an example of how I set my states for my animations in each event (All of this is in the PlayerOne Blueprint)

So basically I want the same thing to happen in player two (same way of doing animation etc), however, im having trouble spawning in player two and allowing it to work on the same keyboard as player one

There are multiple ways to do it. Here si mine :

I created a new blueprint class from the Pawn class. I just put a Camera inside of it and place it in my level, it will be the main camera and is not supposed to move.
Then I create a new Controller that will handle my input. So that everything is used in my level, I create a new GameMode and I set the value of PlayerController as my new controller class and the value of PlayerPawn to my new pawn class.

In the player controller I set 2 variables : Player1 and Player2, of the class of your character blueprint. At BeginPlay in the controller I would spawn 2 players and populate my 2 variables with references to them (Here I created a new blueprint class called BP_SpawnPoint with a variable telling which player should spawn here, and I place these blueprints in my level to have 2 spawn points).

And then create a set of input like “MoveRightP1” and “MoveRightP2”. When MoveRightP1 is called I call a move function on my Player1 variable and when MoveRightP2 is called I would call a move function on Player2.

I got it working on my end but the problem is hat the 2 players are not properly “posessed” so you cannot use “AddMovementInput” on them and you would have to create your own movement functions. Here I used AddActorLocalOffset but that’s not the best function to handle collision I think, you would probably want to create your own functions.

So with the camera and the spawn points my level looks like this :

And I end up with this when I play with each character being moved by its own set of keys that I set up in my project settings :

I updated my answer with screenshot, tell me if it’s more understandable for you =)

So to clarify, no script is needed in the spawn right you are just using that as a reference as to where the players should spawn. Ok so in my character BP I have all these functions which set a certain state this state then correlates to a specific animation so how do I still set these states (using enum) and still cause the animations to play back in my character if I’m doing all my movement script etc in the controller eg.

how would I then set up my jump in the controller and still have it properly jump and do the animation?
Sorry if my questions are getting out of hand I just want to be exact and 100% sure that I understand :smiley:

Furthermore, for my camera, I can just use a camera actor yes? Also, I am using 2 different characters and I see you are spawning the same character in the spawn script does it still work if im using two different ones. Finally (Sorry for so many questions) what is this connection Array Element to selection?

Awesome, So in saying that how would I then set the state after the jump so that back in my character BP I can have the animation play any idea? Also my shoot command basically says play this animation but also spawn the projectile from a point on the character so again it ties back to my first comment, how do I set my animations projectile spawn points that are on the character from the controller?
Ps. Still cant figure out what is connecting Array Elements and Selection :smiley:

It’s ok, I was also confused with the Pawn/Controller thing when I started. The thing is that in the templates that Epic gives you (like the 2D sideScroller Template), the input are managed in the pawn but in fact it’s often best practice to manage them in the controller.

You would then do something like that in the controller : On P1_Jump event (or whatever name you use), I call a jump event or function in my Player1 variable.
And same thing on player 2. So you can keep a unique function for jumping in your character, you just have to call the right one in the controller.
Here is the script in my controller :

246603-controllerjump.png

And of course with that you would have 2 jump functions in your input, onr for P1 and one for P2, something like that :

246604-inputs.png

So basically you character only has one jump or move function and you controller is deciding on which player to call it depending on the input received.

Yes, here I called jump function just to show you that you can call any function in the character. In your case you would call a custom function that would do exactly what you did in your second screenshot. But instead of being called in the pawn on an input, it’s called by the player controller on an input, so that the controller can say “P1 execute the jump behaviour” or “P2 execute the jump behaviour”.

So you can actually keep your function on your pawn, just replace the “E” input box by a custom event that you create and your player controller can call.

That’s not the simplest thing to explain but I hope you get the idea ^^
:

  • Repond to input in the controller
  • The controller choose which function to call on which player

Awesome thank you for everything I should be able to take it from here one last thing though and its going to sound kind of stupid but its late and im still a bit confusedalt text

When I pull off the array elements what exactly do I search for in the node search thingy because I type player and I cannot find this connector thing to the selector. Maybe its me just being a bit idiotic :smiley:

Awesome, sorry that that went over my head. Thank you for everything man if I do have any questions ill be sure to flick you a message. Until next time :smiley:

Hi there, thanks for the answer im still very new to unreal so as helpless as I sound do you mind maybe showing me some screenshots because my knowledge with player controllers etc is quite small as I just got into developing. To some extent, I understand what you are saying but if you could spare a moment to elaborate that would be great
:smiley:

Dunno if ive missed something but for some odd reason it doesn’t spawn my players

Oh, player is a variable I have created in my SpawnPoint blueprint. And I made it public so that, when I put a SpawnPoint in the level, I can tell it “you are the player 0 spawn” or “you are the player 1 spawn”.

Glad I could help, don’t forget to mark my post as answer so that the question is tagged as “resolved” =) Good luck in your project!

You have to create a new GameMode blueprint class and inside it you set the PlayerPawn value to the CameraPawn you created and the PlayerController value to the Controller you created. Then you have to tell your level that you want to use this game mode :
You can override the current game mode of the map in the world settings of your level. Or you can set this game mode as the default game mode for every map in the Project settings → Maps and Modes section

Hey guys i’m also trying to get this to work and i’m having problems as i’m very new to Game Dev.

So at the moment i have 2 charaters being spawned on 1 of the Spawn-points for some reason and i have only 1 input working for 1 of the characters. I also have errors to do with setting player input to 1 and 2 i guess?

P.s Yes i’m making the basic pong game and making my own version :stuck_out_tongue: just to start getting basics done and yes i know Its normally 1 Player vs A.i but i’m gonna have both options!