Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Splitscreen Input Problem

Discussion in 'Editor & General Support' started by Adjaar7, Dec 26, 2020.

  1. Adjaar7

    Adjaar7

    Joined:
    Jan 6, 2020
    Posts:
    22
    Hi there! I am working a split screen top down pirate game, and I have a problem. When both players are moving, player 1 can not turn right. The two players are identical and hold the same scripts, they are simply in different positions. Player1 uses WASD and Player 2 uses IJKL.

    Code (CSharp):
    1. public class PlayerMovement : MonoBehaviour
    2. {
    3.    
    4.     Rigidbody2D rb;
    5.     public int playerNumber;
    6.     private string Player;
    7.  
    8.  
    9.     [SerializeField]
    10.     public float accelerationPower = 1f;
    11.     [SerializeField]
    12.     public float steeringPower = 1f;
    13.     float steeringAmount, speed, direction;
    14.    
    15.  
    16.     void Start()
    17.     {
    18.         rb = GetComponent<Rigidbody2D>();
    19.         Player = ("Player") + playerNumber;
    20.     }
    21.  
    22.     void FixedUpdate()
    23.     {
    24.  
    25.         steeringAmount = Input.GetAxis("Horizontal" + playerNumber);
    26.         speed = Input.GetAxis("Vertical" + playerNumber) * accelerationPower;
    27.        
    28.         direction = Mathf.Sign(Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.up)));
    29.         rb.rotation += steeringAmount * steeringPower * rb.velocity.magnitude * direction;
    30.  
    31.         rb.AddRelativeForce(-Vector2.up * speed);
    32.  
    33.         rb.AddRelativeForce(-Vector2.right * steeringAmount);
    34.  
    35.     }
    Both players move and player 1 moves normally if player 2 is not moving forward. If player 2 is only turning horizontally, player 1 is unaffected, but the moment I add forward movement ("i' key), player 1 can only move forward or turn left.

    I know it's hard to try and solve my problem with only a small script of player movement and no access to my inspector, but any help would be appreciated. I don't even know where to begin for this problem since player2 is perfectly fine and they are virtually identical.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    First make a super-simple test to identify if it is possibly key jamming (rollover) that is causing your problem:

    https://en.wikipedia.org/wiki/Rollover_(key)

    Not all keyboards can read all keys at all times. In fact, most consumer keyboards can only read three or four keys at a time maximum. Don't agonize over trying to fix your code if the above is happening, and it's easy to write a simple "what keys are being pressed" blob of code to prove it.
     
    PraetorBlue likes this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    You don't even need to bother writing something to test rollover either (although doing so may help you understand the problem a bit better). There are tools on the internet to test this quickly and easily: http://gadzikowski.com/nkeyrollover.html
     
  4. Adjaar7

    Adjaar7

    Joined:
    Jan 6, 2020
    Posts:
    22
    I had no idea that this was a thing! Is there a fix? It's weird because some order of keys pressed allow me to keep adding more keys (8+), but when I do the two forward keys (w & i), the d key is disabled immediately. Should player 2 just be binded to a controller? This is hobby not commercial, and I am just learning. Thanks you two for this assistance
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Buy a more expensive keyboard I suppose. It's not exactly a bullet point on most keyboard products however.

    It has to do with how the keys are wired physically, and to some extent with how complex the decoder logic is in the support circuitry, and basically just any other software limits in the keyboard firmware. It could even be limited by the OS but it's not possible to really know without taking apart a particular system and tracking down what is going on.