- Home /
Recognize number of players and set differents input for an action
Hi all,
In one of my school project, I am doing a little prototype where I can play in multiplayer, on the same screen. Here's a quick example of my project, it's one of my sources : Soccer Physics - http://www.youtube.com/watch?v=QqhFlF8cxKE
Right now, I'm looking to know the more optimized way to instantiate a certain number of players (given from a variable that I would get from a menu) and give them respective controls to play? My actions are simple, they can only jump. I will be using WiiMotes to play my game (that's another case for later) but for now I'll just use the buttons on my keyboard.
So I though a script similar to
var nbPlayers:int = 10;
var playerPrefab:GameObject;
var arrayCtrl[] = "Fire1", "Fire2"; //etc...
for(var i:int=0; i>=nbPlayers; i++){
var newPlayer[i] = Instantiate(playerPrefab, Vector3(0,0,0), Quaternion.identity);
newPlayer[i].GetComponent(newPlayer).setJump(arrayCtrl[i]);
}
Do I have to make an Array with the maximum controls available (if 16players max, I would put 16 buttons down) and instantiate them in a "for" via the number of players that has been entered? ATM I'm using an Update() in the Control.JS script on my player to know if he press the "Jump" then he jumps.
Maybe I could send in my Controls script the specific key to press to jump for each indiviual players? And that my Control.JS script would only do an Update() and check if the key that has been sent in the script is pressed and if yes do the action?
Feel free to add or show me a way more optimized. And in advance, thanks for your help!
Your answer
![](https://koobas.hobune.stream/wayback/20220613165801im_/https://answers.unity.com/themes/thub/images/avi.jpg)