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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How do I make a ball move only with 4 keys?

Discussion in 'Editor & General Support' started by Spillehaj, Oct 25, 2015.

  1. Spillehaj

    Spillehaj

    Joined:
    Oct 13, 2015
    Posts:
    16
    Hey,

    I'm trying make a game. It is made so it has an option to play 4 players at once on 1 computer. So what i need is to make the balls move, one with WASD, one with arrows, one with UHJK and one with GVBN or something like that. But I don't know how :-/ I made this

    void FixedUpdate ()
    {
    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");

    Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

    rb.AddForce (movement * speed);

    It moves with WASD and arrows but i want one that i can easily change so i can make 4 scripts to with the different keycodes.

    Help is very apprecaited! Thanks!
     
  2. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    836
    There's lots of ways of doing this, but the simplest (I think) would be to just have a variable that defines which player the ball belongs to, and get input based off that. You'll first need to setup separate movement axes for each player (use the InputManager in Edit->Project Settings->Input. In the example below I named them "Horizontal1", "Horizontal2", and so on.

    Code (csharp):
    1.  
    2. // Set this 0-3 depending on which player the ball belongs to
    3. [SerializeField]
    4. int player = 0;
    5.  
    6. void FixedUpdate ()
    7. {
    8.     float moveHorizontal = 0;
    9.     float moveVertical = 0;
    10.    
    11.     if (player == 0)
    12.     {
    13.         moveHorizontal = Input.GetAxis ("Horizontal1");
    14.         moveVertical = Input.GetAxis ("Vertical1");
    15.     }
    16.     else if (player == 1)
    17.     {
    18.         moveHorizontal = Input.GetAxis ("Horizontal2");
    19.         moveVertical = Input.GetAxis ("Vertical2");
    20.     }
    21.     else if (player == 2)
    22.     {
    23.         moveHorizontal = Input.GetAxis ("Horizontal3");
    24.         moveVertical = Input.GetAxis ("Vertical3");
    25.     }
    26.     else if (player == 3)
    27.     {
    28.         moveHorizontal = Input.GetAxis ("Horizontal4");
    29.         moveVertical = Input.GetAxis ("Vertical4");
    30.     }
    31.  
    32.     Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    33.  
    34.     rb.AddForce (movement * speed);
    35. }
    Also, on the forum you can use the [ code ] tags to put your code in so that it's easier to read.
     
  3. Spillehaj

    Spillehaj

    Joined:
    Oct 13, 2015
    Posts:
    16
    @Iron-Warrior
    Thanks, so I can just use that code and all my 4 balls will be possible to roll at the same time?
     
  4. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    836
    Each player will control one individually and separately, yes.
     
  5. Spillehaj

    Spillehaj

    Joined:
    Oct 13, 2015
    Posts:
    16