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

Input System Movement using Gamepad

Discussion in 'Input System' started by PaRsOnIsPhErE, Jan 8, 2020.

  1. PaRsOnIsPhErE

    PaRsOnIsPhErE

    Joined:
    Dec 19, 2018
    Posts:
    19
    Hi guys, I have recently switched from GMS 1.4 to Unity (reasons being GMS 1.4 no longer has support and has become too costly for my needs).

    I'm creating a 2d local multiplayer game, and am trying to implement the new input system.

    I'm having some trouble though... I'm trying to determine the x and y position of the left stick in my action map > move. Below is a screenshot of my action map, I am using an official wired xbox controller.
    upload_2020-1-8_14-20-38.png

    Below is the code for moving my ship using the rigidbody physics. I've set moveInput to be a public vector2 so I can see it's values in the editor, along with my h and v values, although once I know it's working, that will be changed.

    Code (CSharp):
    1. public void OnMove(InputValue moveValue)
    2.     {
    3.         moveInput = moveValue.Get<Vector2>();
    4.     }
    5.  
    6.     void Update()
    7.     {
    8.         h = -moveInput.x;
    9.         v = moveInput.y;
    10.  
    11.         Vector2 speed = transform.up * (v * acceleration);
    12.         rb.AddForce(speed);
    13.  
    14.         float direction = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.up));
    15.         if (direction >= 0.0f)
    16.         {
    17.             rb.rotation += h * steering * (rb.velocity.magnitude / 5.0f);
    18.             rb.AddTorque((h * steering) * (rb.velocity.magnitude / 10.0f));
    19.         }
    20.         else
    21.         {
    22.             rb.rotation -= h * steering * (rb.velocity.magnitude / 5.0f);
    23.             rb.AddTorque((-h * steering) * (rb.velocity.magnitude / 10.0f));
    24.         }
    25.  
    26.         Vector2 forward = new Vector2(0.0f, 0.5f);
    27.         float steeringRightAngle;
    28.         if (rb.angularVelocity > 0)
    29.         {
    30.             steeringRightAngle = -90;
    31.         }
    32.         else
    33.         {
    34.             steeringRightAngle = 90;
    35.         }
    36.  
    37.         Vector2 rightAngleFromForward = Quaternion.AngleAxis(steeringRightAngle, Vector3.forward) * forward;
    38.         Debug.DrawLine((Vector3)rb.position, (Vector3)rb.GetRelativePoint(rightAngleFromForward), Color.green);
    39.  
    40.         float driftForce = Vector2.Dot(rb.velocity, rb.GetRelativeVector(rightAngleFromForward.normalized));
    41.  
    42.         Vector2 relativeForce = (rightAngleFromForward.normalized * -1.0f) * (driftForce * 10.0f);
    43.  
    44.         rb.AddForce(rb.GetRelativeVector(relativeForce));
    45.     }
    Below is a photo of my ships inspector tab. I have been reading and re-reading everything I can find, but think I've read too much and have now become overly confused...
    upload_2020-1-8_14-26-32.png

    Thank you in advance for any help!

    Anyone who does help, a beer and a warm meal is on the house (establishment of my choosing) if we were to ever to meet in person!
     

    Attached Files:

  2. ben4d85

    ben4d85

    Joined:
    Dec 26, 2018
    Posts:
    47
    Welcome to Unity! The below is untested, but see if it gets you any further.

    Code (CSharp):
    1. public void OnMove(InputAction.CallbackContext context)
    2. {      
    3.     moveInput = context.ReadValue<Vector2>();
    4. }
     
  3. PaRsOnIsPhErE

    PaRsOnIsPhErE

    Joined:
    Dec 19, 2018
    Posts:
    19
    Hi ben4d85! That works like a charm!

    I had previously tried that function, but clearly hadn't been setting moveInput within it.

    Thank you!
     
    ben4d85 likes this.