- Home /
Unity Input System local multiplayer
Hi everyone,
I am using Unitys Input System to create a local multiplayer game. I have the current code configured for my inputs which works great for one player, the code below rotates the camera
void Movement()
{
cameraInputMovement = playerActionControls.Camera.OnRotation.ReadValue<Vector2>();
//Multiply the X and Y Axis by the speed the player should move at
x += cameraInputMovement.x * xSpeed * Time.deltaTime;
y -= cameraInputMovement.y * ySpeed * Time.deltaTime;
//clamps the Y angle to the limits specified
y = ClampAngle(y, yMinLimit, yMaxLimit);
//Use the X and Y axis for rotation
Quaternion rotation = Quaternion.Euler(y, x, 0);
//Get the distance from the target
Vector3 negDistance = new Vector3(0, height, -distance);
Vector3 position = rotation * negDistance + target.position;
transform.rotation = rotation;
transform.position = position;
}
The problem is, when I add another player in using the Player Input managers split screen functionality, the camera rotation input controls both players camera rotation. I figured out that I can assign Unity Events to the Player Input component on the player to get around this problem, so I changed my code to the below:
//This function is assigned on the player input component as a unity event
public void OnRotate(InputAction.CallbackContext context)
{
cameraInputMovement = context.ReadValue<Vector2>();
Debug.Log("moving");
}
void Movement()
{
//cameraInputMovement = playerActionControls.Camera.OnRotation.ReadValue<Vector2>();
//Multiply the X and Y Axis by the speed the player should move at
x += cameraInputMovement.x * xSpeed * Time.deltaTime;
y -= cameraInputMovement.y * ySpeed * Time.deltaTime;
//clamps the Y angle to the limits specified
y = ClampAngle(y, yMinLimit, yMaxLimit);
//Use the X and Y axis for rotation
Quaternion rotation = Quaternion.Euler(y, x, 0);
//Get the distance from the target
Vector3 negDistance = new Vector3(0, height, -distance);
Vector3 position = rotation * negDistance + target.position;
transform.rotation = rotation;
transform.position = position;
}
Unfortunately, with this method, no inputs are picked up at all, not even the Debug Message. It should be noted that the Player Input component is on the player, but this camera script is on the Main Camera, which is a child component of the player, not sure if that matters.
Does anyone know why no inputs are being picked up?
Sounds to me like the input settings are somehow configured wrong, if it's not even firing the event. How do the Input settings look like that you've set up for the camera rotation?
I've attached a screenshot of the input actions on the player, these input actions work if I directly access the input action in the Update method, it's only when I want to call it from the Unity Events that it's not working. I must be missing something
For some reason the picture can't be loaded :/ . So let's do it this way: I am guessing you are using Delta [Mouse] as the binding path? Do you have any Interactions or Processors applied?
Answer by The-Peaceful · May 11, 2021 at 06:07 PM
Hey there,
I think I finnaly found out what's going on :D So by default your 'Player Controls' action map is enabled, but 'OnRotation' is on the 'Camera' action map. Action maps don't invoke events if they are disabled. So either put 'OnRotation' in the 'Player Controls' action map or put a second PlayerInput component on the camera with the Default Map set to 'Camera'. OR if you just need one at a time you could switch around between action maps like show here https://youtu.be/ElqAoS2FEDo?t=417
Think that should do it, let me know if it works :D
Hey,
Thanks a million for all your help so far, what you're saying makes perfect sense. I have changed my setup so that I am only calling from the Player Controls Action Map now. Unfortunately, I am still experiencing the same issue, I am getting no input back in the scene.
I am invoking other Unity Events on the Player Object as well which all work with no issue, it's only the camera one that' causing a problem.
I also tried adding a player input on the camera and used the Camera default map, but the same issue remains. I'm beginning to think that this may be a bug.
I'll continue to troubleshoot it though and if you have any other suggestions in the meantime, I'd be happy to hear them out. Thanks again
Ok, that's pretty strange. You could try out the Input Debugger to see if maybe there is no input detected or if there are any other strange things happening there?
https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Debugging.html
Thanks for that, the delta mouse is definitely being recognized by the debugger. Just as a test, I mapped my OnRotate function to the Movement Action (which works for my movement) and funnily, the camera now rotates when I'm using the WSAD keys.
So the problem must be with the Delta function, I'm going to try separate out my x and y values into 2 separate functions to check if that works, but I think I'm finally getting somewhere, if I manage to get it working, I'll revert back and accept your answer :)
Answer by JayFitz91 · May 12, 2021 at 10:29 PM
Hi Everyone,
Sorry for spamming this with my comments, however, I managed to find what was actually causing the issue. In my comments above, I mentioned that after removing and creating the Input Action map with the same Actions, the mouse now worked as expected, however, the issue started occurring again once I configured the Control Scheme.
I then realized that this was happening because I was only configuring the Keyboard in the Control Scheme, so mouse input was being registered as it's own device (which now explains why it was adding another player when I clicked the Fire button)
So in order to resolve my issue, I had to create a control scheme with both Keyboard and Mouse:
After this, my mouse started working as normal again because the Keyboard and Mouse scheme were being recognised as one input.
Hopefully this helps anyone else that has this issue.