Search Unity

Multiplayer Joystick question

Discussion in 'PSM' started by tauntonian, Jun 24, 2014.

  1. tauntonian

    tauntonian

    Joined:
    Jun 22, 2014
    Posts:
    4
    To start - I'm very new at this. I watched a tutorial series to get a feel for Unity; it wasn't PSM-focused, now I need help. I'm trying to make a local multiplayer game where Player01 controls an object on the left side of the screen with the left joystick and Player02 controls an object on the right with the right stick.

    Here is the code I started to try to adapt for the Vita but I'm stuck:
    Code (JavaScript):
    1. #pragma strict
    2. //In here was KeyCode, but I want joystick input
    3. var moveUp : ;
    4. var moveDown : ;
    5.  
    6. var speed : float = 10;
    7.  
    8. function Update (Player01)
    9. {
    10.     if (Input.GetAxis ("LeftV"))
    11.     {
    12.         rigidbody2D.velocity.y = speed;
    13.     }
    14.     else if (Input.GetAxis ("LeftV"))
    15.     {
    16.         rigidbody2D.velocity.y = speed *-1;
    17.     }
    18.     else
    19.     {
    20.         rigidbody2D.velocity.y = 0;
    21.     }
    22. }
    23.  
    24. function Update (Player02)
    25. {
    26.     if (Input.GetAxis ("RightV"))
    27.     {
    28.         rigidbody2D.velocity.y = speed;
    29.     }
    30.     else if (Input.GetAxis ("RightV"))
    31.     {
    32.         rigidbody2D.velocity.y = speed *-1;
    33.     }
    34.     else
    35.     {
    36.         rigidbody2D.velocity.y = 0;
    37.     }
    38. }
     
  2. PeteD

    PeteD

    Joined:
    Jul 30, 2013
    Posts:
    71
    First off merge the two functions and put them in the fixed update method of a gameobject.
     
  3. GrahamReeves

    GrahamReeves

    Unity Technologies

    Joined:
    Apr 8, 2014
    Posts:
    30
  4. PeterD

    PeterD

    Joined:
    Feb 6, 2013
    Posts:
    120
    For clarity these are the inputs you need to be reading

    Input.GetAxis("Left Stick Horizontal"));
    Input.GetAxis("Left Stick Vertical"));
    Input.GetAxis("Right Stick Horizontal"));
    Input.GetAxis("Right Stick Vertical"));
     
  5. PeteD

    PeteD

    Joined:
    Jul 30, 2013
    Posts:
    71
    Oh and you can use the base scene from the PSM input demo to correctly map the controls.
     
  6. tauntonian

    tauntonian

    Joined:
    Jun 22, 2014
    Posts:
    4
    OK, I've made some adjustments and some progress. The game will get the input, but both players will move if either stick is pressed. Also, I can't figure out how to get them to move down, they will only move up(negative). Updated code:
    Code (JavaScript):
    1.  #pragma strict
    2.  
    3. var speed : float = 10;
    4.  
    5. function Update ()
    6. {
    7.     if (Input.GetAxis("left stick vertical axis"))
    8.     {
    9.         rigidbody2D.velocity.y = speed;
    10.     }
    11.     else if (Input.GetAxis("left stick vertical axis"))
    12.     {
    13.         rigidbody2D.velocity.y = speed *-1;
    14.     }
    15.     else if (Input.GetAxis("right stick vertical axis"))
    16.     {
    17.         rigidbody2D.velocity.y = speed;
    18.     }
    19.     else if (Input.GetAxis("right stick vertical axis"))
    20.     {
    21.         rigidbody2D.velocity.y = speed *-1;
    22.     }
    23.     else
    24.     {
    25.         rigidbody2D.velocity.y = 0;
    26.     }
    27. }
    Also:
    Where can I find the demo? I looked in all the installation folders but no luck.
     
  7. eriQue

    eriQue

    Unity Technologies

    Joined:
    May 25, 2010
    Posts:
    595
  8. tauntonian

    tauntonian

    Joined:
    Jun 22, 2014
    Posts:
    4
  9. GrahamReeves

    GrahamReeves

    Unity Technologies

    Joined:
    Apr 8, 2014
    Posts:
    30
    you're using the GetAxis function wrong. Have a read of the documentation for inputs:
    http://docs.unity3d.com/ScriptReference/Input.GetAxis.html

    GetAxis returns -1...1, so you should use that directly on velocity.y.
    You're code currently says

    Code (JavaScript):
    1.  
    2. if ( LeftStickMoved )
    3. rigidbody y = speed
    4. else if ( LeftStickMoved )
    5. rigidbody y = -speed
    6.  
    so the 2nd if never hits as the first one will.

    you want something more like
    Code (JavaScript):
    1.  
    2. rigidbody2D.velocity.y = speed * Input.GetAxis("left stick vertical axis");
    3.  
    As for moving both players... you only ever reference "rigidbody". Is this script attached to both players? (in which case you're running the same "move on stick change" code on both, and need a different script referring to different inputs for each players)
    Or is rigidbody the name of one of the objects?
     
    tauntonian likes this.
  10. tauntonian

    tauntonian

    Joined:
    Jun 22, 2014
    Posts:
    4
    Thank you everyone. Got it working. I made a separate script for each player and changed the usage of getaxis. I'm sure I'll be back with more questions
     
    PeteD likes this.