For those who want split-keyboard play: -Make 2 control schemes of type Keyboard on your InputActionAsset. -Assign the different keys to the corresponding control schemes (e.g. WASD and Arrow Keys) -Now, you can either manually spawn the two players using their respective control schemes: var p1 = PlayerInput.Instantiate(playerPrefab, controlScheme: "KeyboardLeft", device: Keyboard.current); var p2 = PlayerInput.Instantiate(playerPrefab, controlScheme: "KeyboardRight", device: Keyboard.current); forum.unity.com/threads/2-players-on-same-input-device.763949/ docs.unity3d.com/Packages/com.unity.inputsystem@1.0/api/UnityEngine.InputSystem.PlayerInput.html -Or once the first or second player has joined (you can subscribe to the Player Joined event), switch the control scheme on the player with SwitchCurrentControlScheme(…) docs.unity3d.com/Packages/com.unity.inputsystem@1.0/api/UnityEngine.InputSystem.PlayerInput.html#UnityEngine_InputSystem_PlayerInput_SwitchCurrentControlScheme_System_String_UnityEngine_InputSystem_InputDevice___ -Make sure to disable the auto switch component on the Player Input so it doesn’t switch between different devices/control schemes.
When I try to put the name of the player prefab I use in place of the name in there (playerPrefab) unity just spits out a compiler error saying that it doesn't know what the player name is doing there. So is there something else that that playerPrefab represents?
The player prefab in this example is the same one fed into the Player Input component as I showed in the video, it is just a representation of the player with it’s camera and optional UI. You probably need a reference to your prefab in the script for it to work properly.
So for the subscribe to the Player Joined Event. Do I create a public void SwitchPlayer() method which then uses the playerInput.SwitchCurrentControlScheme("Controller", Keyboard.current); ? I was hoping for an example in the documentation and a little confused on how to implement this.
@@StevieVu You can press Behaviour>Invoke Unity Events>In your script make a function like so public void OnPlayerJoined(PlayerInput playerInput) { // your code here } and in the behaviors section of the player input component drag your script and select the function you made (this is one of the ways, you can also use SendMessages although I don’t recommend that approach)
Ok so now whenever I try and do this it says that device is not an overload of instantiate. So then what's wrong because I just copied and pasted the code above and switched it out with my varibles in some places. So I don't really know how to fix this.
This is the only tutorial that worked for me. I've been trying to make a multiplayer game for a long time now but I never understood the new input system. Great tutorial!
Helpful vid. Two things... 1: KEYBOARD BASS HYPE!!!! 2: If you add inputs while in a control scheme, it'll automatically tick that control scheme's checkbox. Weird way to say it, but you probably get what I mean ; ]
This was fantastic, thank you very much for this. It solved a few problems for me, somewhere along the way my old control scheme was tangled up and it was controlling 2 characters at once, and for the life of me I couldnt figure out where it was coming from. Following this tutorially helped immensely with my issue so thanks again
Dope tutorial. Been messing with new input system about 3 years ago and it was such a mess back then. Had to create my own library for handling all of the devices connecting and reconnecting, there was no documentation and the one exisitng was thrash. But here with 15 mins tutorial I can get it all working like a charm. Finally Unity made a closure to this system and it can be easily used.
I found a way of getting using the same pad/keyboard method - add two action maps, and put each of the actions relating to each player in them. Don't forget to add each of the player's settings in the events
Could you please do a video on how to do local multiplayer with existing objects? For example, I have two teams and I want a player to be able to control each team.
Hi, thanks for the amazing tutorial. However, I have issues with my gamepad input and keyboard input mixing up with eachother. My movement is functioning as it should, but anything else such as shooting/crouching/jumping inputs are applied on both of the players whenever I start using gamepad. Thanks for the help
Dynamic "CallbackContext" doesnt show for me (9:26) Im on 2021.3.22 . Ive gone back and forth over this, but its not showing. anyone else come across this, or know why? My OnJump and OnMove are public voids too.
This tutorial has saved me! I was scared I'd have to watch 30 minutes of someone explaining how to code multiplayer in a near-silent voice. Thanks! I have one question; so far, I've watched up to 9:25, why is nothing coming up when I click on the function button but "MonoScript"? Do you have any idea why?
anyone know how to fix: Assets\scripts\PlayerController.cs(14,13): error CS0246: The type or namespace name 'vector2' could not be found (are you missing a using directive or an assembly reference?)
This is the only tutourial i found that actually worked and it was the most simple one lol. I do have a question tho. I'm striving to make a local co-op fps game and it looks a bit weird when everyone has the same character model. How do I set up specific players to spawn in. Ex- player one is a cube, player 2 is a sphere, player three is an octopus, and player 4 is a snake. Those aren't my models btw just trying to make myself clear lmao
Hi! This is great solution and all works fine, but i have a possibly dumb question. I decided to go a little further and set spawn points for each player, and here I am a little confused due to the number of fields and options in the player input manager. As I understand it, it is necessary to create the necessary empty objects on the stage for the spawn points themselves, but where and how is the player’s spawn function called and how can I set the spawn point? Should i use manual join behavior? How to setup Player Join Events correctly? May be there is a simple solution but there is so many questions in my head right now)))
Awesome tutorial! How are players removed / split screen put back to non-split? I want players to join, die, go away, and join anew, like old arcade games where the second player can stop playing for a while and then hit start to come back in. But it looks like PlayerInputManager has no way to manually invoke or force a player to leave.
Yes good point, I made a new video recently explaining the differences between the different action types and upon further investigation Value is preferable for most situations :)
Great Video Sam thank you! I have some questions though, I hope you don't mind answering to an old video. I'm currently working on a local split-screen multiplayer mode for my game and I want to primarily use controllers for gameplay. I have an issue where both characters are controller by one controller. The spawning works on the second controller but I don't have separate control over the other character which is very weird plus the documentation doesn't have a good way of explaining this, do you have any possible suggestions to fix this? Do I have to create a second control scheme for player 2 and bind the same buttons on the controller and manually set the scheme in code? Thanks in advance :)
How do I get the start and release Button information in OnJump(). Want to make a jump like in mario holding down to increase hight. But I cannot understand how to do it with this. I know how to do it with singel player using WasPerformedThisFrame, and WasReleasedThisFrame(). I just don't know how to write it with local 2 player in mind.
Hey, if you are ok with answering on an old video (lol) I have a question. So is there a difference in this between 3d and 2d games? I am making a local multiplayer smash bros type game except with guns. It's very rudimentary as of now. I have the controls working on my gamepad and it runs smoothly, the problem comes in when I try to go multiplayer. Currently when I spawn a player in I have no input. So for example I built my input and controls, turned the player into a prefab, deleted player from scene, and added the prefab to the player manager, and now controls don't work anymore. I even used debug statements and the actions arnt being called for some reason. Note: I am using two controllers as I plan to have up to 8 players. If you respond I will love you forever lol Edit: I subbed btw. This is the best input system tutorial out there rn!
I try to respond to all the comments 🙂 The concept should work regardless of 2D or 3D mode. What control schemes are you using for both players? Make sure the actions are being enabled. You can use the Input Debugger to see if your actions are enabled and what values they are getting th-cam.com/video/ICh1ZEaVUjc/w-d-xo.html We also have a Discord channel where you can ask in the help channel with more detail discord.gg/Eu9462Jq
Great tutorial, Simple and easy to understand but I have a glitch that when a controller disconnects and then reconnects it creates a new instance of the player and doesn’t reconnect to the old player is there anyway to fix this or is it something completely different
Just wondering, is there any way to modify the follow camera so it uses a anchor for the camera rotation like in a camera orbit script? Currently the scripts conflict with another since the CameraFollow uses the player transform as a target, and I want to be able to use the split screen look with a rotating camera for each player. Thank you again for your help on my original comment!
This is one of the most well explained tutorials I've found! Thank you so much for sharing and teaching. I have a question though. I followed all the steps but my keyboard and my controller move both of my characters in sync. Any idea why? I've created two different control schemes
@@samyam wow thank you for the fast reply! Yeah I took off the auto switch in the inspector for my character's prefab. :( maybe it's just buggy but I thought I'd ask! I'll try it in a fresh project file, maybe that will fix it.
Hello! I followed this tutorial and it was great. What I want to know is how to start my game with the 2 characters already spawned in and able to be controlled. I was trying to make a script that would simply press spacebar and the 'A' button once when the scene loads, but that proved to be too difficult. Thank you!
@@walbobo I want them to share the same camera. Its kinda like a half court basketball game. Where you pick one or two players. They spawn at the same time. They have the same controls, but use their own gamepad. But as I type this out, I think you mean Player1 will get Player1 controls, then player2 will be assigned player 2 controls. That still leaves it pretty ambiguous as either controller can be picked up by either player controller
Thanks alot for this tutorial this helped alot, I can't seem to figure out how to assign a gamepad to a gameobject with PlayerInput in the scene without spawning it when pressing a button. I need them both to exist at the start of the game
I don’t think it supports it directly yet unfortunately but there is a workaround you can do: onewheelstudio.com/blog/2022/2/3/split-screen-multi-player-with-unitys-new-input-system
Great Tutorial! I have one question though, how do I achieve the same effect with one keyboard? I made to schemes with different inputs however when I press the button to join the player only one will join.
Thanks! I haven't done this personally, but this forum might help you out: forum.unity.com/threads/multiple-players-on-keyboard-new-input-system.725834/ Which seems they create a virtual keyboard for the second player Virtual device: "This allows you to create your own Devices, which can be useful for testing purposes, or for creating virtual Input Devices which synthesize input from other events." docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Devices.html
Hi, I managed to go around that by programming my own Input Manager, since the unity one supports only 1 player for one device, thus mutiplayer on one kayboard is a bit tricky. I created this little script: public GameObject playerPrefab1; public GameObject playerPrefab2; PlayerInput p1; PlayerInput p2; void Start() { p1 = PlayerInput.Instantiate(playerPrefab1, controlScheme: "Player1", pairWithDevice: Keyboard.current); p2 = PlayerInput.Instantiate(playerPrefab2, controlScheme: "Player2", pairWithDevice: Keyboard.current); } This will create two players at the beginning with the specified schemes on the same keyboard. Hope this helps!
Hey Sam, great video once again ! I was wondering what way is the better (offers most of features/best performance) between using the Component "Player Input" or writing directly in script controls.Player.Jump.performed += _ => my_function() ? Thanks a lot
I don’t think you should worry about the performance between the two, but you can always measure it with the profiler if you’d like. The second option you can subscribe to individual events and it’s more clear (.started, .canceled, .performed) while the PlayerInput gives you one event when the action has been triggered. Up to your preference :) I personally like the first option since it’s directly in the code. docs.unity3d.com/Packages/com.unity.inputsystem@0.2/api/UnityEngine.InputSystem.Plugins.PlayerInput.PlayerInput.html
@@samyam Thanks a lot for your answer ! I'm gonna try the first option ;) And try to figure out how to do implement the local multiplayer feature with it !
Okay i'll admit, I'm learning unity and I did the rolla ball tutorial to make my game, because currently I want my first game to be a coop marble game, but I'm struggling, is it possible to add the split screen portion of this code that allows a second player to join without any conflicts?
@@samyam Maybe this is a question more for your discord, but i'm having difficulties getting my marble to, actually roll like a marble, modifying input movements hasn't done any real changes to my marble (the character's) movement.
Is there a reason why your using context.action.performed instead of using context.performed? i find that when i use context.action.performed the function gets called twice as opposed to just using context.performed where the function is only called once
Great tutorial! But is there a way to get the cube to not be floating off the ground? EDIT: figured out the answer if anyone else has the same issue, chagne the skin width on the "Character Controller" component to 0
How would i go about doing this but with multiple characters? To specify i am meaning like two different characters in a fighting game have same buttons but different results. How would one do this? My apologies for poor explanation
There are several ways you can possibly do it, what comes to mind is mapping an input action to a certain character attack depending on the character. So you can have a generic scriptable object that you can extend and have an instance per character of the fighting game. And so for each instance/character, you could swap out the function that it calls (you can more easily do this via an interface, and have a class that implements this interface with its attack type). This might help you: learn.unity.com/tutorial/create-an-ability-system-with-scriptable-objects#5cf5ecededbc2a36a1bd53b7 www.radermatthew.com/scriptable-object-practice
Nice video, but its is possible this with two o more players each playing with a gamepad?, do you need will create two o more schema controllers with gamepad?
You can add two different Keyboard control schemes with different keys and each player will correspond to one of the schemes: forum.unity.com/threads/local-multiplayer-with-split-keyboard-support.825603/
@@samyam i made it in the most inefficient way by creating double player control script(I was desperate) and splitting screen in traditional way. thanks for the link and awesome tutorials :D
I have this all working with a Rigidbody2d instead of a Movement Controller. Both characters (keyboard/controller) run & jump, but after running the game once, the PlayerInput component in my Player prefab loses the "Actions: Input Action Asset" reference to PlayerControls. Anyone have an idea why this happens?
Seems to be a bug, what version of the input system are you using (and what Unity version)? Try upgrading the input system version if you can. Should be fixed in version 1.1-preview.3. forum.unity.com/threads/playerinput-component-loses-its-reference-to-an-inputactionasset.861118/ forum.unity.com/threads/input-action-asset-gets-unassigned-from-instantiated-prefab.1004381/
Make sure there aren't any errors in the code (Window>General>Console). Also, make sure your events are properly set in the PlayerInput component and that the functions to read the input are getting read correctly. You can also use the input debugger (th-cam.com/video/ICh1ZEaVUjc/w-d-xo.html) to verify that your input actions are getting the input read correctly.
@@samyam Thank you, I guess i'm going to just go through your video again. I really appreciate the quick reply also btw so thank you. One more thing is I've learned a couple different ways to use the inputs, would u say this is one of the most effective ways? Thanks a bunch, your new fan.
Thank you 😁 Well up until recently I was Generating a Script from the Input Action Asset and making an instance of that in the code, which is a good way, however since the code is generated, it can’t be changed at runtime (the input action asset) which makes stuff like rebinding keys hard. So I’d recommend using the PlayerInput component and most of the documentation suggests. You can query your actions directly instead of using the events as so playerInputActions.actions[“Look”].ReadValue.... and you can cache the Look action (playerInputActions.actions[“Look”]) to avoid having that long call at the beginning
@@samyam So I got It working, then I followed your smooth camera tutorial, nice btw, but now It will let me join in two players with just the keyboard not even having my controller plugged in and my players wont move anymore. Do you have any idea what might be wrong with it? It also says don't destroy on load in the hierarchy and when i click on it the second player joins in, but cant move.
Hmm maybe you removed some sort of setting in the process? It’s hard to tell without seeing the project. You can join our discord and post your steps more in depth in the help channel to see what we can do if you’d like discord.gg/bvQEGk7
Check the Input debugger, Window>Analysis>Input Debugger and see if your gamepad is printing out a value for the jump button, if it’s not you might have set up the input action or script wrong (or there might be a bug with unity’s input manager)
I am 99% sure that I will not get an answer but my problem is that no functions are appearing in player events except monoscript Am I missing something?
thank you so much! i was able to create a basketball demo for my project i did it in 2D, i am now trying to do a 2d shooting game problem is, my play-testers had much confusion on who was who how can I change the color of a cube/sprite when they join?
Great! The PlayerInputManager has a onCreatePlayer that you can use to manually spawn the prefab and edit it as necessary docs.unity3d.com/Packages/com.unity.inputsystem@0.2/api/UnityEngine.InputSystem.Plugins.PlayerInput.PlayerInputManager.html
Ah you wouldn’t edit the prefab directly, once you make an instance of it via Instantiate then you can get it’s material or renderer component and change the color docs.unity3d.com/Manual/InstantiatingPrefabs.html answers.unity.com/questions/1524909/renderer-material-color-changing-with-script.html
the shoot read value bool is incorrect, it reads a type of float. Also you are overriding that value with triggered right after, I would either delete that function and use triggered or remove the triggered part
The prefab is just a grouping of objects with specific settings already set, we only control one player in the player prefab, but we also want to be including our camera when instantiating the character. However we do not want the camera to be a child of the player directly because that can cause issues with the camera movement, so in this case it’s best to make another parent which has the camera and the player as children. And then that is made into a prefab which we instantiate. Hope that helps.
I spent whole 2 days making the mechanics of the game and the player... until when i use the player input manager and it doesn’t work for me it just spawns another player but both controllers can control him, sometimes it doesn’t spawn a player just the two controllers moving one character... i give up...😓😪
Hi, I really need some help, I currently have it all working everything is set up like yours except I have the wasd and arrowkeys instead. I go to play it and only one of the characters will move! The second character just won't move at all, If anyone could help me with this, that would be amazing
Hey! Did you check out my pinned comment on this video? That might help you out with players using the same device (keyboard). Make sure you have two different control schemes and auto switch off in the player input component
@@samyam So i Fixed it! I saw your comment earlier but it didnt work, then I did some research and replaced 'device: Keyboard.current' with 'pairWithDevice: Keyboard.current' (from your comment) And it worked! Thanks for the help tho!
How is it possible to create multiple players with one keyboard? I followed your instructions and created a second control scheme for ijkl, but everytime i want to "spawn" a second player nothing happens and the first player follows the instructions of ijkl aswell. Great video btw :)
So I tried it out and I think the issue is the Player Input component is setting the default scheme on the Player and since it's using the same device it is not "including" the other control scheme. You could detect whenever the new control scheme is pressed and set the default for that player, however this article says creating a virtual keyboard worked: medium.com/@gb6d/two-players-one-keyboard-how-to-enable-multiple-players-in-the-same-device-with-unity-cd57853edc3f
@@samyam Thank you so much for the quick answer! I wish there was a more graceful way to solve the problem, but even if i use multiple different keyboards unity still detects them as one input :/
hey i am very stuck on how to spawn players at certain places and not just to the origin of a prefab. eg. i want2 cars to spawn at two different parking spots in a parking lot.... if anyone has any ideas pls let me know
You can either manually instantiate the players yourself (in the PlayerInputManager set to manual then you can instantiate with PlayerInput.Instantiate(...) ) and place them in the positions that you'd like, or you can subscribe to the PlayerJoined event from the PlayerInputManager, and that will give you a reference to the PlayerInput component of the player that was just spawned, and you can get the gameobject from that (I believe with .gameObject as usual), and change the position to what you'd like.
I was struggling to make one keyboard for many (at least 2 players) players to work, but... there are so many bugs there. PlayerInputManager is missing reference for new players when the second player use the same device (keyboard). There are some hacky solutions with creating Virtual Device Keyboard, but... meh. For now, don't try to use same device for many players with this system. If I'm wrong, comment below. Anyway, really good tutorial with keyboard+gamepad setup!
Yeah it’s a bit buggy, I recommend making a new control scheme in the input action asset for each of the players, that was it doesn’t overlap. So make 2 of type Keyboard and Mouse and the first one assign maybe the WASD keys and the other the arrow keys (or whatever controls you are using) I go over making a control scheme here th-cam.com/video/f0wax8LufqQ/w-d-xo.html
Yeah it should! There might be some clashing issues where one control might override the movement of the other, but if you do have that issue check my pinned comment, I provide a similar example when 2 people want to use the same keyboard.
Make sure you have that same variable name in your input action and in your script, it seems like there is something named differently. It’s difficult to tell without seeing the code, but you can double click the error message and go to the line where the error is to give you an indication.
You mean in my other video for smooth camera? No I didn’t implement that but shouldn’t be too bad for something simple. There is a way to use cinemachine with local multiplayer www.monkeykidgc.com/2020/12/unity-split-screen-multiplayer-using-cinemachine-follow-cameras.html
Sorry Sam I have seen the notification of your response to my vector3 spawn question…but the comment have disappear and I can’t read your answer 😭. Can you copy and paste under this comment and I hope it don’t disappear this time.
In the player input manager there’s a player joined event you can subscribe to, and it gives you the player input component of the spanned character. You can get the gameobject from that (just call .gameObect on that component) and set the transform to what you like. Hope that helps!
For those who want split-keyboard play:
-Make 2 control schemes of type Keyboard on your InputActionAsset.
-Assign the different keys to the corresponding control schemes (e.g. WASD and Arrow Keys)
-Now, you can either manually spawn the two players using their respective control schemes:
var p1 = PlayerInput.Instantiate(playerPrefab,
controlScheme: "KeyboardLeft", device: Keyboard.current);
var p2 = PlayerInput.Instantiate(playerPrefab,
controlScheme: "KeyboardRight", device: Keyboard.current);
forum.unity.com/threads/2-players-on-same-input-device.763949/
docs.unity3d.com/Packages/com.unity.inputsystem@1.0/api/UnityEngine.InputSystem.PlayerInput.html
-Or once the first or second player has joined (you can subscribe to the Player Joined event),
switch the control scheme on the player with SwitchCurrentControlScheme(…)
docs.unity3d.com/Packages/com.unity.inputsystem@1.0/api/UnityEngine.InputSystem.PlayerInput.html#UnityEngine_InputSystem_PlayerInput_SwitchCurrentControlScheme_System_String_UnityEngine_InputSystem_InputDevice___
-Make sure to disable the auto switch component on the Player Input so it doesn’t switch between different devices/control schemes.
When I try to put the name of the player prefab I use in place of the name in there (playerPrefab) unity just spits out a compiler error saying that it doesn't know what the player name is doing there. So is there something else that that playerPrefab represents?
The player prefab in this example is the same one fed into the Player Input component as I showed in the video, it is just a representation of the player with it’s camera and optional UI. You probably need a reference to your prefab in the script for it to work properly.
So for the subscribe to the Player Joined Event.
Do I create a public void SwitchPlayer() method which then uses the playerInput.SwitchCurrentControlScheme("Controller", Keyboard.current); ?
I was hoping for an example in the documentation and a little confused on how to implement this.
@@StevieVu You can press Behaviour>Invoke Unity Events>In your script make a function like so
public void OnPlayerJoined(PlayerInput playerInput) {
// your code here
}
and in the behaviors section of the player input component drag your script and select the function you made (this is one of the ways, you can also use SendMessages although I don’t recommend that approach)
Ok so now whenever I try and do this it says that device is not an overload of instantiate. So then what's wrong because I just copied and pasted the code above and switched it out with my varibles in some places. So I don't really know how to fix this.
This is the only tutorial that worked for me. I've been trying to make a multiplayer game for a long time now but I never understood the new input system. Great tutorial!
Thank you! Taught me how it works, got it working on my machine, and was ONLY 15 MINUTES?!?!?! Mind-blowing educational skill.
These are easily some of the best Unity tutorials on TH-cam. Great video!
Man. You never fail to give exactly what Im looking for. Thank you so much for these videos
Helpful vid.
Two things...
1: KEYBOARD BASS HYPE!!!!
2: If you add inputs while in a control scheme, it'll automatically tick that control scheme's checkbox. Weird way to say it, but you probably get what I mean ; ]
Thank you!!
This was fantastic, thank you very much for this. It solved a few problems for me, somewhere along the way my old control scheme was tangled up and it was controlling 2 characters at once, and for the life of me I couldnt figure out where it was coming from. Following this tutorially helped immensely with my issue so thanks again
Dope tutorial. Been messing with new input system about 3 years ago and it was such a mess back then. Had to create my own library for handling all of the devices connecting and reconnecting, there was no documentation and the one exisitng was thrash. But here with 15 mins tutorial I can get it all working like a charm. Finally Unity made a closure to this system and it can be easily used.
Yes it’s been a long time coming, thanks for watching!
Wonderful, I was secretly hoping you would make a video one day on exactly THIS! Thank you :-)
Nice! 😄
Best Unity tutorials on YT. Thank you so much for this!
Woah! What a timing! Just what I was looking for. Brilliant tutorial.
It's a magic! Thank you so much! I love you samyam, you are the best!
That was a very good tutorial, you got to the point and didn't waste my time, well done.
I found a way of getting using the same pad/keyboard method - add two action maps, and put each of the actions relating to each player in them. Don't forget to add each of the player's settings in the events
Excellent as always 🙏🏾
Exactly what I was looking for! Great video!
Thanks!
Thanks Samyam! Really appreciate this video :)
Could you please do a video on how to do local multiplayer with existing objects? For example, I have two teams and I want a player to be able to control each team.
Hi, thanks for the amazing tutorial. However, I have issues with my gamepad input and keyboard input mixing up with eachother. My movement is functioning as it should, but anything else such as shooting/crouching/jumping inputs are applied on both of the players whenever I start using gamepad. Thanks for the help
Dynamic "CallbackContext" doesnt show for me (9:26) Im on 2021.3.22 .
Ive gone back and forth over this, but its not showing. anyone else come across this, or know why? My OnJump and OnMove are public voids too.
This tutorial has saved me! I was scared I'd have to watch 30 minutes of someone explaining how to code multiplayer in a near-silent voice. Thanks!
I have one question; so far, I've watched up to 9:25, why is nothing coming up when I click on the function button but "MonoScript"? Do you have any idea why?
Have you found the solution?
@@marmadon6341 I found it but finished the game and don't have the code.
anyone know how to fix: Assets\scripts\PlayerController.cs(14,13): error CS0246: The type or namespace name 'vector2' could not be found (are you missing a using directive or an assembly reference?)
does anyone know why in the player controller function why onMove and onJump dont show up? (im doing this in 2D mode btw)
I am using a fps controller but when i add two players both of them move.
Thanks that was exactly what i needed
Can you add two controllers? If yes then how?
I have 2 gamepads (XBox) and 2 characters, but they both move at the same time. How do I prevent this?
I'm having the same issue
Thank you very much! Very useful tutorial!
Ok I followed the tutorial but my keyboard and controller inputs have become combined. I have no idea how to fix it
Hello, I wonder how to use 2 joysticks to control different players, is that supported in vanilla unity?
This is the only tutourial i found that actually worked and it was the most simple one lol. I do have a question tho. I'm striving to make a local co-op fps game and it looks a bit weird when everyone has the same character model. How do I set up specific players to spawn in. Ex- player one is a cube, player 2 is a sphere, player three is an octopus, and player 4 is a snake. Those aren't my models btw just trying to make myself clear lmao
Check my pinned comment! You can manually instantiate players with different prefabs as needed :)
Love your Tutorials :)
On point and up to date. Great tutorial.
how is this up to date my friend?
how can you use this tutorial to set up 2 controllers with the same input?
How can u add players depending on the controller u are using? kinda like street fighter game? Is it really difficult? Thanks.
Thanks! This was really useful!
Hi! This is great solution and all works fine, but i have a possibly dumb question. I decided to go a little further and set spawn points for each player, and here I am a little confused due to the number of fields and options in the player input manager. As I understand it, it is necessary to create the necessary empty objects on the stage for the spawn points themselves, but where and how is the player’s spawn function called and how can I set the spawn point? Should i use manual join behavior? How to setup Player Join Events correctly? May be there is a simple solution but there is so many questions in my head right now)))
Awesome tutorial!
How are players removed / split screen put back to non-split? I want players to join, die, go away, and join anew, like old arcade games where the second player can stop playing for a while and then hit start to come back in. But it looks like PlayerInputManager has no way to manually invoke or force a player to leave.
Nice tutorial, thanks for sharing.
Yes good point, I made a new video recently explaining the differences between the different action types and upon further investigation Value is preferable for most situations :)
Great Video Sam thank you!
I have some questions though, I hope you don't mind answering to an old video.
I'm currently working on a local split-screen multiplayer mode for my game and I want to primarily use controllers for gameplay. I have an issue where both characters are controller by one controller. The spawning works on the second controller but I don't have separate control over the other character which is very weird plus the documentation doesn't have a good way of explaining this, do you have any possible suggestions to fix this? Do I have to create a second control scheme for player 2 and bind the same buttons on the controller and manually set the scheme in code? Thanks in advance :)
Hey! Make sure to check the pinned comment on this video I think it will help solve your issue! :)
@@samyam Yeah, it finally works now thank you 🙏
You have a new subscriber!
Thank you!
How could I get the position data from the spawned players?
I have a question: does make sense to add a different Player Input Manager for each player?
Best tutorial on the new input system i've watched, it helped me a lot
is this possible with 2 controllers
Great video, thank you!
Is there a way to assign device to player that is ALREADY in the scene ?
How do I get the start and release Button information in OnJump(). Want to make a jump like in mario holding down to increase hight. But I cannot understand how to do it with this. I know how to do it with singel player using WasPerformedThisFrame, and WasReleasedThisFrame(). I just don't know how to write it with local 2 player in mind.
THANKK YOUUU
Thank you.
I made this in unity 2d and The player goes through objects with collider 2ds, how could I change this? Great tutorial by the way.
Would this kind of approach work for A VR setup on Android running quest/pico headset?
Hey, if you are ok with answering on an old video (lol) I have a question. So is there a difference in this between 3d and 2d games? I am making a local multiplayer smash bros type game except with guns. It's very rudimentary as of now. I have the controls working on my gamepad and it runs smoothly, the problem comes in when I try to go multiplayer. Currently when I spawn a player in I have no input. So for example I built my input and controls, turned the player into a prefab, deleted player from scene, and added the prefab to the player manager, and now controls don't work anymore. I even used debug statements and the actions arnt being called for some reason. Note: I am using two controllers as I plan to have up to 8 players. If you respond I will love you forever lol
Edit: I subbed btw. This is the best input system tutorial out there rn!
I try to respond to all the comments 🙂 The concept should work regardless of 2D or 3D mode. What control schemes are you using for both players? Make sure the actions are being enabled. You can use the Input Debugger to see if your actions are enabled and what values they are getting th-cam.com/video/ICh1ZEaVUjc/w-d-xo.html
We also have a Discord channel where you can ask in the help channel with more detail discord.gg/Eu9462Jq
Very good tutorial. Thank you so much :)
Great tutorial, Simple and easy to understand but I have a glitch that when a controller disconnects and then reconnects it creates a new instance of the player and doesn’t reconnect to the old player is there anyway to fix this or is it something completely different
Just wondering, is there any way to modify the follow camera so it uses a anchor for the camera rotation like in a camera orbit script? Currently the scripts conflict with another since the CameraFollow uses the player transform as a target, and I want to be able to use the split screen look with a rotating camera for each player. Thank you again for your help on my original comment!
What do I need to change if I want two different game characters?
This is one of the most well explained tutorials I've found! Thank you so much for sharing and teaching.
I have a question though. I followed all the steps but my keyboard and my controller move both of my characters in sync. Any idea why? I've created two different control schemes
Hmm in the player input component did you deselect switch between devices? I found the player input manager to be buggy sometimes
@@samyam wow thank you for the fast reply! Yeah I took off the auto switch in the inspector for my character's prefab. :( maybe it's just buggy but I thought I'd ask! I'll try it in a fresh project file, maybe that will fix it.
Yeah just make sure in the input action to select keyboard or controller for that corresponding action 🙂
Hello! I followed this tutorial and it was great. What I want to know is how to start my game with the 2 characters already spawned in and able to be controlled. I was trying to make a script that would simply press spacebar and the 'A' button once when the scene loads, but that proved to be too difficult. Thank you!
Did you ever find that out? I'm looking for something simliar
@@realmcraftchronicles Can't remember exactly, but I'd assume as long as each has their own camera and unique input system that it would work
@@walbobo I want them to share the same camera. Its kinda like a half court basketball game. Where you pick one or two players. They spawn at the same time. They have the same controls, but use their own gamepad. But as I type this out, I think you mean Player1 will get Player1 controls, then player2 will be assigned player 2 controls. That still leaves it pretty ambiguous as either controller can be picked up by either player controller
Thanks alot for this tutorial this helped alot, I can't seem to figure out how to assign a gamepad to a gameobject with PlayerInput in the scene without spawning it when pressing a button. I need them both to exist at the start of the game
I need help with this too! I can't find anything that helps.
Great video. Thanks a lot :D
thanks for the the awesome tutoril
Hi Samyam,
I can appreciate this was made about a year ago now, do we know if cinemachine supports this yet?
I don’t think it supports it directly yet unfortunately but there is a workaround you can do:
onewheelstudio.com/blog/2022/2/3/split-screen-multi-player-with-unitys-new-input-system
Great Tutorial! I have one question though, how do I achieve the same effect with one keyboard? I made to schemes with different inputs however when I press the button to join the player only one will join.
Thanks! I haven't done this personally, but this forum might help you out:
forum.unity.com/threads/multiple-players-on-keyboard-new-input-system.725834/
Which seems they create a virtual keyboard for the second player
Virtual device: "This allows you to create your own Devices, which can be useful for testing purposes, or for creating virtual Input Devices which synthesize input from other events."
docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Devices.html
Hi, I managed to go around that by programming my own Input Manager, since the unity one supports only 1 player for one device, thus mutiplayer on one kayboard is a bit tricky. I created this little script:
public GameObject playerPrefab1;
public GameObject playerPrefab2;
PlayerInput p1;
PlayerInput p2;
void Start()
{
p1 = PlayerInput.Instantiate(playerPrefab1, controlScheme: "Player1", pairWithDevice: Keyboard.current);
p2 = PlayerInput.Instantiate(playerPrefab2, controlScheme: "Player2", pairWithDevice: Keyboard.current);
}
This will create two players at the beginning with the specified schemes on the same keyboard. Hope this helps!
Thanks!
thx for this vid it helped me a lot :)
Hey Sam, great video once again ! I was wondering what way is the better (offers most of features/best performance) between using the Component "Player Input" or writing directly in script controls.Player.Jump.performed += _ => my_function() ? Thanks a lot
I don’t think you should worry about the performance between the two, but you can always measure it with the profiler if you’d like. The second option you can subscribe to individual events and it’s more clear (.started, .canceled, .performed) while the PlayerInput gives you one event when the action has been triggered. Up to your preference :) I personally like the first option since it’s directly in the code.
docs.unity3d.com/Packages/com.unity.inputsystem@0.2/api/UnityEngine.InputSystem.Plugins.PlayerInput.PlayerInput.html
@@samyam Thanks a lot for your answer ! I'm gonna try the first option ;) And try to figure out how to do implement the local multiplayer feature with it !
your fingers are machines!!
🖖
Okay i'll admit, I'm learning unity and I did the rolla ball tutorial to make my game, because currently I want my first game to be a coop marble game, but I'm struggling, is it possible to add the split screen portion of this code that allows a second player to join without any conflicts?
What sort of conflict are you having? I recommend checking the pinned comment on this video
@@samyam Maybe this is a question more for your discord, but i'm having difficulties getting my marble to, actually roll like a marble, modifying input movements hasn't done any real changes to my marble (the character's) movement.
Is there a reason why your using context.action.performed instead of using context.performed? i find that when i use context.action.performed the function gets called twice as opposed to just using context.performed where the function is only called once
Would this still work if I used 2 game padd
Great tutorial! But is there a way to get the cube to not be floating off the ground?
EDIT: figured out the answer if anyone else has the same issue, chagne the skin width on the "Character Controller" component to 0
How would i go about doing this but with multiple characters? To specify i am meaning like two different characters in a fighting game have same buttons but different results. How would one do this? My apologies for poor explanation
There are several ways you can possibly do it, what comes to mind is mapping an input action to a certain character attack depending on the character.
So you can have a generic scriptable object that you can extend and have an instance per character of the fighting game.
And so for each instance/character, you could swap out the function that it calls (you can more easily do this via an interface, and have a class that implements this interface with its attack type).
This might help you:
learn.unity.com/tutorial/create-an-ability-system-with-scriptable-objects#5cf5ecededbc2a36a1bd53b7
www.radermatthew.com/scriptable-object-practice
@@samyam Thanks!
Nice video, but its is possible this with two o more players each playing with a gamepad?, do you need will create two o more schema controllers with gamepad?
I think it'd be best to create a different schema for each player so the Player Input Manager doesn't get confused.
Awesome video and really well explained! Instant sub from me :)
Can we add 2 or more player using just keyboard
Like p1: W A S D
And p2: Arrow keys
You can add two different Keyboard control schemes with different keys and each player will correspond to one of the schemes:
forum.unity.com/threads/local-multiplayer-with-split-keyboard-support.825603/
@@samyam i made it in the most inefficient way by creating double player control script(I was desperate) and splitting screen in traditional way. thanks for the link and awesome tutorials :D
What about 2 controllers?
That's what I'm here to find out, but I think I have it figured out. 🤔 I'll post a video about it if I get it 👍
Great tutorial, could you make a tutorial on swipe detection with the new input system?
I'll put it on my list!
I have this all working with a Rigidbody2d instead of a Movement Controller. Both characters (keyboard/controller) run & jump, but after running the game once, the PlayerInput component in my Player prefab loses the "Actions: Input Action Asset" reference to PlayerControls. Anyone have an idea why this happens?
Seems to be a bug, what version of the input system are you using (and what Unity version)? Try upgrading the input system version if you can. Should be fixed in version 1.1-preview.3.
forum.unity.com/threads/playerinput-component-loses-its-reference-to-an-inputactionasset.861118/
forum.unity.com/threads/input-action-asset-gets-unassigned-from-instantiated-prefab.1004381/
Can i use this in Genarate C# class?
If possible can there be a part 2
What kind of content would you be looking to see in a part 2?
samyam maybe a 2d local multiplayer Game ,if you can 😊
Gotcha, I'll take it into account for a possible future video!
samyam thx
@@bossfakealeks12345 that would be good
So I followed you and when I went to move my player, it did nothing. Any help would be awesome. I really want to learn these subjects.
Make sure there aren't any errors in the code (Window>General>Console). Also, make sure your events are properly set in the PlayerInput component and that the functions to read the input are getting read correctly. You can also use the input debugger (th-cam.com/video/ICh1ZEaVUjc/w-d-xo.html) to verify that your input actions are getting the input read correctly.
@@samyam Thank you, I guess i'm going to just go through your video again. I really appreciate the quick reply also btw so thank you. One more thing is I've learned a couple different ways to use the inputs, would u say this is one of the most effective ways? Thanks a bunch, your new fan.
Thank you 😁 Well up until recently I was Generating a Script from the Input Action Asset and making an instance of that in the code, which is a good way, however since the code is generated, it can’t be changed at runtime (the input action asset) which makes stuff like rebinding keys hard. So I’d recommend using the PlayerInput component and most of the documentation suggests. You can query your actions directly instead of using the events as so
playerInputActions.actions[“Look”].ReadValue....
and you can cache the Look action (playerInputActions.actions[“Look”]) to avoid having that long call at the beginning
@@samyam So I got It working, then I followed your smooth camera tutorial, nice btw, but now It will let me join in two players with just the keyboard not even having my controller plugged in and my players wont move anymore. Do you have any idea what might be wrong with it? It also says don't destroy on load in the hierarchy and when i click on it the second player joins in, but cant move.
Hmm maybe you removed some sort of setting in the process? It’s hard to tell without seeing the project. You can join our discord and post your steps more in depth in the help channel to see what we can do if you’d like discord.gg/bvQEGk7
help, when I connect my Xbox controller the player keeps jumping for some reason
Check the Input debugger, Window>Analysis>Input Debugger and see if your gamepad is printing out a value for the jump button, if it’s not you might have set up the input action or script wrong (or there might be a bug with unity’s input manager)
I am 99% sure that I will not get an answer but my problem is that no functions are appearing in player events except monoscript
Am I missing something?
When you add a PlayerController script in events, drag it from the inspector tab, not from the project
thank you so much! i was able to create a basketball demo for my project i did it in 2D, i am now trying to do a 2d shooting game
problem is, my play-testers had much confusion on who was who
how can I change the color of a cube/sprite when they join?
Great! The PlayerInputManager has a onCreatePlayer that you can use to manually spawn the prefab and edit it as necessary
docs.unity3d.com/Packages/com.unity.inputsystem@0.2/api/UnityEngine.InputSystem.Plugins.PlayerInput.PlayerInputManager.html
Ah you wouldn’t edit the prefab directly, once you make an instance of it via Instantiate then you can get it’s material or renderer component and change the color
docs.unity3d.com/Manual/InstantiatingPrefabs.html
answers.unity.com/questions/1524909/renderer-material-color-changing-with-script.html
@samyam Help, it won't let me jump (or in my case, shoot) It tells me this error (if you can help me!) : InvalidOperationException while executing 'started' callbacks of 'PC / Shoot [/ Keyboard / m]'
UnityEngine.InputSystem.LowLevel. C__DisplayClass7_0: b__0 (NativeInputUpdateType, NativeInputEventBuffer *)
UnityEngineInternal.Input.NativeInputSystem: NotifyUpdate (NativeInputUpdateType, IntPtr) (at C: /buildslave/unity/build/Modules/Input/Private/Input.cs: 117) InvalidOperationException: Cannot read value of type 'Boolean' from control '/ Keyboard / m 'bound to action' PC / Shoot [/ Keyboard / m] '(control is a' KeyControl 'with value type' float ')
UnityEngine.InputSystem.InputActionState.ReadValue [TValue] (System.Int32 bindingIndex, System.Int32 controlIndex, System.Boolean ignoreComposites) (at Library/PackageCache/com.unity.inputsystem@1.0.1/InputSystem/Actions/InputActionStatem/Actions/InputActionStatem/Actions/InputActionStatem/Actions/InputActionStatem 2020)
UnityEngine.InputSystem.InputAction + CallbackContext.ReadValue [TValue] () (at Library/PackageCache/com.unity.inputsystem@1.0.1/InputSystem/Actions/InputAction.cs: 1433)
PlayerControllerPC.OnAtack (UnityEngine.InputSystem.InputAction + CallbackContext context) (at Assets / Scripts / PlayerControllerPC.cs: 43)
UnityEngine.Events.InvokableCall`1 [T1] .Invoke (T1 args0) (at C: /buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent.cs: 207)
UnityEngine.Events.UnityEvent`1 [T0] .Invoke (T0 arg0) (at C: /buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent/UnityEvent_1.cs: 58)
UnityEngine.InputSystem.Utilities.DelegateHelpers.InvokeCallbacksSafe [TValue] (UnityEngine.InputSystem.Utilities.InlinedArray`1 [System.Action`1 [TValue]] & callbacks, TValue argument, System.String callbackName, System.Object context) (at context Library/PackageCache/com.unity.inputsystem@1.0.1/InputSystem/Utilities/DelegateHelpers.cs: 51)
UnityEngine.InputSystem.LowLevel. C__DisplayClass7_0: b__0 (NativeInputUpdateType, NativeInputEventBuffer *)
UnityEngineInternal.Input.NativeInputSystem: NotifyUpdate (NativeInputUpdateType, IntPtr) (at C: /buildslave/unity/build/Modules/Input/Private/Input.cs: 117)
this is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerControllerPC : MonoBehaviour
{
public float Speed = 5f;
Vector2 movement;
private bool shoot = false;
public Rigidbody rb;
private CharacterController controller;
public GameObject bullet;
private void Start()
{
controller = gameObject.GetComponent();
}
private void Update()
{
Vector2 move = new Vector2(movement.x, movement.y);
controller.Move(move * Time.deltaTime * Speed);
if(shoot)
{
Debug.Log("YOu SHOOT:PC");
}
controller.Move(move *Speed * Time.deltaTime);
}
public void OnMove(InputAction.CallbackContext context)
{
movement = context.ReadValue();
}
public void OnAtack(InputAction.CallbackContext context)
{
shoot = context.ReadValue();
shoot = context.action.triggered;
}
private void FixedUpdate()
{
// rb.MovePosition(rb.position + movement * Speed * Time.deltaTime);
}
}
the shoot read value bool is incorrect, it reads a type of float. Also you are overriding that value with triggered right after, I would either delete that function and use triggered or remove the triggered part
Question. Why is there the player parent prefab instead of simply the player prefab? Why use the extra step?
So we can also have the camera and UI if needed under the parent, and when the player is instantiated so will their respective camera and UI
@@samyam Ah. So controlling other components besides just the main character?
The prefab is just a grouping of objects with specific settings already set, we only control one player in the player prefab, but we also want to be including our camera when instantiating the character. However we do not want the camera to be a child of the player directly because that can cause issues with the camera movement, so in this case it’s best to make another parent which has the camera and the player as children. And then that is made into a prefab which we instantiate. Hope that helps.
Good Job
I spent whole 2 days making the mechanics of the game and the player... until when i use the player input manager and it doesn’t work for me it just spawns another player but both controllers can control him, sometimes it doesn’t spawn a player just the two controllers moving one character... i give up...😓😪
Make sure you have 2 separate control schemes for the two different players, check my pinned comment for more information.
@@samyam thanks samyam you’re the best!
Hi, I really need some help, I currently have it all working everything is set up like yours except I have the wasd and arrowkeys instead.
I go to play it and only one of the characters will move!
The second character just won't move at all,
If anyone could help me with this, that would be amazing
I forgot to mention that I used different movement code not sure if that might be doing it
Hey! Did you check out my pinned comment on this video? That might help you out with players using the same device (keyboard). Make sure you have two different control schemes and auto switch off in the player input component
@@samyam So i Fixed it!
I saw your comment earlier but it didnt work, then I did some research and replaced
'device: Keyboard.current'
with
'pairWithDevice: Keyboard.current'
(from your comment)
And it worked!
Thanks for the help tho!
How is it possible to create multiple players with one keyboard? I followed your instructions and created a second control scheme for ijkl, but everytime i want to "spawn" a second player nothing happens and the first player follows the instructions of ijkl aswell. Great video btw :)
So I tried it out and I think the issue is the Player Input component is setting the default scheme on the Player and since it's using the same device it is not "including" the other control scheme. You could detect whenever the new control scheme is pressed and set the default for that player, however this article says creating a virtual keyboard worked:
medium.com/@gb6d/two-players-one-keyboard-how-to-enable-multiple-players-in-the-same-device-with-unity-cd57853edc3f
@@samyam Thank you so much for the quick answer! I wish there was a more graceful way to solve the problem, but even if i use multiple different keyboards unity still detects them as one input :/
Even with the virtual keyboard mode? Maybe you can try posting on the Input System forum with the issue
Hi Sam would you mind making atutorial step by step on character creation using Unity Input System and Bolt together..Thanks..
It’s on my huge list of future videos, perhaps one day I will! 😂
@@samyam glad to see it coming
hey i am very stuck on how to spawn players at certain places and not just to the origin of a prefab. eg. i want2 cars to spawn at two different parking spots in a parking lot....
if anyone has any ideas pls let me know
You can either manually instantiate the players yourself (in the PlayerInputManager set to manual then you can instantiate with PlayerInput.Instantiate(...) ) and place them in the positions that you'd like, or you can subscribe to the PlayerJoined event from the PlayerInputManager, and that will give you a reference to the PlayerInput component of the player that was just spawned, and you can get the gameobject from that (I believe with .gameObject as usual), and change the position to what you'd like.
I was struggling to make one keyboard for many (at least 2 players) players to work, but... there are so many bugs there. PlayerInputManager is missing reference for new players when the second player use the same device (keyboard). There are some hacky solutions with creating Virtual Device Keyboard, but... meh. For now, don't try to use same device for many players with this system. If I'm wrong, comment below.
Anyway, really good tutorial with keyboard+gamepad setup!
Yeah it’s a bit buggy, I recommend making a new control scheme in the input action asset for each of the players, that was it doesn’t overlap. So make 2 of type Keyboard and Mouse and the first one assign maybe the WASD keys and the other the arrow keys (or whatever controls you are using) I go over making a control scheme here th-cam.com/video/f0wax8LufqQ/w-d-xo.html
how to use ui with 2 players
Will this work with 2 controllers?
Yeah it should! There might be some clashing issues where one control might override the movement of the other, but if you do have that issue check my pinned comment, I provide a similar example when 2 people want to use the same keyboard.
@@samyam Ok! Thank you so much!
I get a error saying movementInput/jumped does not exist on the current context
What’s the exact error? Make you there are no typos and you can also try regenerating the script from the input action asset
@@samyam there are no typos
@@samyam the error is
jumped does not exist in the current context
And
movementInput does not exist in the current context
Make sure you have that same variable name in your input action and in your script, it seems like there is something named differently. It’s difficult to tell without seeing the code, but you can double click the error message and go to the line where the error is to give you an indication.
Valeu!
very good
i am also doing this in 2d
does this work with third person cinemachine setup
nvm lol
yep :)
@@samyam I was wondering if the smooth camera script have rotation using the new input system
You mean in my other video for smooth camera? No I didn’t implement that but shouldn’t be too bad for something simple. There is a way to use cinemachine with local multiplayer
www.monkeykidgc.com/2020/12/unity-split-screen-multiplayer-using-cinemachine-follow-cameras.html
@@samyam thankyou this helped a lot
Sorry Sam I have seen the notification of your response to my vector3 spawn question…but the comment have disappear and I can’t read your answer 😭. Can you copy and paste under this comment and I hope it don’t disappear this time.
In the player input manager there’s a player joined event you can subscribe to, and it gives you the player input component of the spanned character. You can get the gameobject from that (just call .gameObect on that component) and set the transform to what you like. Hope that helps!