- Home /
Assign Gamepad To Specific Player Input In New Scene
Hello, I've recently run into a standstill in my game that is like Mario Kart. I have a Menu scene that has a player input system and I need some type of method to get say Player 1's assigned gamepad and assign it in the next scene to Player 1. As of rn, both scenes require a player to press a button to join. The problem with this is that in the Game scene when a player joins the order of who's player 1,2,3,4 get's messed up causing players not to spawn as the character they selected. Plus I want the player to spawn automatically and already be assigned the correct corresponding gamepad. It's rather inconvenient to make all players ready up before the game may begin.
My idea as of rn is to use Gamepad.current and to save this as a global var and assign players for example:
if(Gamepad.current == GameObject.Find("GlobalController").GetComponent<GlobalVariables>.player1AssignedController){
thisTexture = assignedTextureForPlayer;
//etc
}
However, I'm hopeful there's a better way. All ideas and questions are welcomed! Thanks!
Answer by cjdabeast99 · Nov 16, 2021 at 10:13 PM
Solved this issue by using
PlayerInput.Instantiate(spawnPlayer, controlScheme: "Gamepad", pairWithDevice: Gamepad.all[0]);
Here's what I entered:
void Awake()
{
DontDestroyOnLoad(gameObject);
}
private void Update()
{
if (SceneManager.GetActiveScene().name != "Menu" && !spawnedCars)
{
spawnedCars = true;
int i = 0;
while(i < playerCount)
{
PlayerInput spawnedCar = PlayerInput.Instantiate(spawnPlayer, controlScheme: "Gamepad", pairWithDevice: Gamepad.all[i]);
spawnedCar.GetComponent<Car>().myPlayerNum = (i + 1);
foreach (string getTexture in avaterText)
{
if(getTexture.Contains("P" + (i + 1)))
{
SetTexture(spawnedCar.gameObject, getTexture, (i + 1));
}
}
i++;
}
}
}
Using this I set the player component "Car" to get its assigned player number, automatically spawn the player, and know who's controlling who :)
The only issue I've found with this method is if controller 1 disconnects and controller 2 is setting up the game then technically that controller is player Gamepad.all[1] not 0, but the system will set player 1 still to Gamepad 0 causing controller 2 not to be functional since Player 1 is assigned Gamepad0. So if there's any way to deter$$anonymous$$e what gamepad index is being used that'd be great ;)
I'm currently try to do a similar thing as you've done, but I don't quite follow how you fixed this issue.
I've got a main menu for people to join the game or leave by making use of the new Player Input Manager. This all works fine, but when I start the game and switch to a different scene I want to instantiate the amount of players that have joined again. The issue I'm facing is that all the amount of players that have joined only work under one controller instead of staying binded to their own controller.
Do you have a good idea on how to fix this issue?
Your answer
Follow this Question
Related Questions
Setting up inputs for multiple gamepads/controllers 1 Answer
New Unity Input System Getting continuous input 1 Answer
Oculus Rift controller triggering Gamepad 1 Answer
gamepad controls 1 Answer
Logitech Gamepad F310 button map? 1 Answer