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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question 2player input control

Discussion in '2D' started by egeuzun0202, Jul 16, 2020.

  1. egeuzun0202

    egeuzun0202

    Joined:
    Jul 16, 2020
    Posts:
    2
    Can someone help me?
    Code (CSharp):
    1. void ProcessInputs()
    2. {
    3.   float moveX = Input.GetAxisRaw("Horizontal");
    4.   float moveX = Input.GetAxisRaw("Vertical");
    5.  
    6.   moveDirection = new Vector2(moveX, moveY).normalized;
    7. }
    8.  
    When i press the "A" button, players moving at the same time.
    How can i change this?
     
  2. IsaacAM517

    IsaacAM517

    Joined:
    Jun 21, 2020
    Posts:
    12
    Isn't the alternative positive button for horizontal input enabled?
     
  3. Daniel7Mazi

    Daniel7Mazi

    Joined:
    Jul 15, 2020
    Posts:
    8
    You have 2 x float moveX for Vertical and Horizontal. I think there should be moveX and moveY but im not sure.

    Anyway, you can just do controller for 2 players with using for first player WSAD and second player arrows. Just check what button is pressed and move player one or two.
     
  4. egeuzun0202

    egeuzun0202

    Joined:
    Jul 16, 2020
    Posts:
    2
    How can i do that? I made a movement script for player then i duplicated the player after that i added them to my scene. When i press the "WASD" or Arrow keys, they are moving together. How can i convert vertical or horizontal inputs to keycode? That's my first project.
     
  5. Daniel7Mazi

    Daniel7Mazi

    Joined:
    Jul 15, 2020
    Posts:
    8
    Simple way to do this (but im not good in programing so its may be wrong way) is check what button is down. You can do this with Update function, just like that:

    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (Input.GetKey(KeyCode.UpArrow))
    4.         {
    5.             some functionality for rigidbody2d (rb for player 1)
    6.         }
    7.  
    8.         if (Input.GetKey(KeyCode.DownArrow))
    9.         {
    10.            some functionality for rigidbody2d (rb for player 1)
    11.         }
    12.  
    13.       if (Input.GetKey(KeyCode.W))
    14.         {
    15.            some functionality for rigidbody2d (rb for player 2)
    16.         }
    17.     }
    Do public rigidbody2d variables for both players and attach rb components to it for both players. If my way is wrong please somebody correct me.