- Home /
How to configure keys to let to players play
Hi, I want to have two players simultaneously in the screen, I have defined both characters with all their stuff, but I have a problem, they move all both together (of course because they have the same script). What should I do to modify to the script for the second player to let it play with different keys? (for example player 1 with WASD and player 2 with arroykeys)
Answer by loramaru · May 03, 2011 at 03:20 AM
I would suggest making multiple inputs with a standard pattern that includes a player number, e.g. "Player1Left", "Player2Left", "Player1Up", "Player2Up", etc. Then add a player number property to your script and change your code to look for an input based on the player number
var playerId : int;
function Update() { if (Input.GetButtonUp("Player" + playerId + "Left")) { // move player left }
if (Input.GetButtonUp("Player" + playerId + "Up")) {
// move player up
}
}
This allows the user to continue to customize using the standard configuration interface and allows for easy scaling up to more than 2 players.
Answer by Thomas 7 · May 02, 2011 at 10:56 PM
You could add a boolean UseArrowKeys and then check in your code if you use WASD or arrow keys. Make sure 1 player object has the boolean true and the other one false.
Answer by ShinyDarkStone · May 03, 2011 at 02:54 AM
you could create a class named playerControls that define which keys use each one on the constructor, then you can create a "configure keys" screen and let the user configure his keyboard using the each class.(lets say playerOneControl and playerTwoControl) Its a little complicated but more flexible and you can add the number of players you want, also add joystick support.
Its a ltl long to expolain, but you sould use the GUI to create the menu and the buttons on the menu to set on the players class the keys.(its not hard to code and i think it worth it)
Your answer
Follow this Question
Related Questions
co-op game? 1 Answer
How to make a simple co-op (2-player) Game? 1 Answer
Unity networking tutorial? 6 Answers
How to check if key is pressed 1 Answer
Check if there is a keyboard input (not a specified key input) 2 Answers