Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question Adding a duplicate player with different inputs

Discussion in 'Input System' started by camendola, Feb 1, 2023.

  1. camendola

    camendola

    Joined:
    Jan 14, 2023
    Posts:
    46
    Hello!

    As stated above, I'm looking for a simple way to duplicate my main player but add different inputs for his controls. I'm currently moving his motor torque with the "vertical axis" and his steering power with "horizontal axis". Which, as far as I know, means uparrow, down arrow - right arrow, left arrow.

    Just wondering how to create this duplicate player to control him with different inputs, if that makes sense. Any help appreciated.
     
  2. rdjadu

    rdjadu

    Joined:
    May 9, 2022
    Posts:
    120
    One way: add two control schemes. Have first player on one, have second player on the other. Disable auto switching.
     
  3. camendola

    camendola

    Joined:
    Jan 14, 2023
    Posts:
    46
    Thanks man! I got really lucky and found a great script to separate my controllers. This is what it looks like now

    Code (csharp):
    1.  
    2. / Update is called once per frame
    3.     void FixedUpdate()
    4.     {
    5.         foreach (var wheel in wheels)
    6.         {
    7.             var vertical = 0;
    8.             if (Input.GetKey(KeyCode.UpArrow)) vertical = 1;
    9.             if (Input.GetKey(KeyCode.DownArrow)) vertical = -1;
    10.  
    11.             wheel.motorTorque = vertical * motorPower;
    12.         }
    13.         {
    14.             for (int i = 0; i < wheels.Length; i++)
    15.             {
    16.                 if (i < 2)
    17.                 {
    18.                     var horizontal = 0;
    19.                     if (Input.GetKey(KeyCode.RightArrow)) horizontal = 1;
    20.                     if (Input.GetKey(KeyCode.LeftArrow)) horizontal = -1;
    21.                     wheels[i].steerAngle = horizontal * steerPower;
    22.                 }
    23.             }
    versus
    Code (csharp):
    1.  
    2.  
    3.         foreach (var wheel in wheels)
    4.         { var vertical = 0;
    5.             if (Input.GetKey(KeyCode.R)) vertical = 1;
    6.             if (Input.GetKey(KeyCode.C)) vertical = -1;
    7.  
    8.             wheel.motorTorque = vertical * motorPower;
    9.         }
    10.         {
    11.             for (int i = 0; i < wheels.Length; i++)
    12.             {
    13.                 if (i < 2)
    14.                 {
    15.                     var horizontal = 0;
    16.                     if (Input.GetKey(KeyCode.G)) horizontal = 1;
    17.                     if (Input.GetKey(KeyCode.D)) horizontal = -1;
    18.                     wheels[i].steerAngle = horizontal * steerPower;
    19.  
    20.         foreach (var wheel in wheels)
    21.         { var vertical = 0;
    22.             if (Input.GetKey(KeyCode.R)) vertical = 1;
    23.             if (Input.GetKey(KeyCode.C)) vertical = -1;
    24.  
    25.             wheel.motorTorque = vertical * motorPower;
    26.         }
    27.         {
    28.             for (int i = 0; i < wheels.Length; i++)
    29.             {
    30.                 if (i < 2)
    31.                 {
    32.                     var horizontal = 0;
    33.                     if (Input.GetKey(KeyCode.G)) horizontal = 1;
    34.                     if (Input.GetKey(KeyCode.D)) horizontal = -1;
    35.                     wheels[i].steerAngle = horizontal * steerPower;
    36. }
    37.  
     
  4. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Store the KeyCodes in variables. That way you can re-use the script for both players, instead of having to copy the entire thing. Also makes adding changes a lot easier.