- Home /
New Input System How to get Left Stick Axis
Hi, I am developing a mobile game and trying to add gamepad support to it, I've set up all the buttons but couldn't figure out how to set up movement (with left stick of gamepad).
My game is a 2d game, so only movement is to the left and right, but the problem is; I don't know how to get the left stick/left axis and left stick/right axis, I want to get those axis and their min/max values so I can check according to them to move the character to the left or right.
If anyone can help me about it I'll be so glad, thanks in advance!
Answer by rh_galaxy · Sep 04, 2021 at 12:24 PM
Here is a try.
Code to put in Update() or FixedUpdate()
Gamepad gamepad = Gamepad.current;
if (gamepad != null)
{
Vector2 stickL = gamepad.leftStick.ReadValue();
//stickL.x will be -1.0..1.0 (for full left to full right)
//stickL.y will be -1.0..1.0 (for full down to full up)
}
It works! Thank you so so much, I've been trying solve this and now it works like a charm. Here is the code I am using if anyone else needs it;
Gamepad gamepad = Gamepad.current;
if (gamepad != null)
{
Vector2 stickL = gamepad.leftStick.ReadValue();
//stickL.x will be -1.0..1.0 (for full left to full right)
//stickL.y will be -1.0..1.0 (for full down to full up)
if (stickL.x < 0 && !freezePlayer)
{
theRB.velocity = new Vector2(moveSpeed * stickL.x, theRB.velocity.y);
}
else if(stickL.x > 0 && !freezePlayer)
{
theRB.velocity = new Vector2(moveSpeed * stickL.x, theRB.velocity.y);
}
else if (!pushCheck)
{
theRB.velocity = new Vector2(0, theRB.velocity.y);
}
if(stickL.x > 0 && !m_FacingRight && !freezePlayer)
{
Flip();
}
if(stickL.x < 0 && m_FacingRight && !freezePlayer)
{
Flip();
}
}
Your answer
Follow this Question
Related Questions
Steering Wheel / joystick GetAxis trouble 0 Answers
Is there a way to manually change the Input.Axis values? 2 Answers
How to make new Axis? 2 Answers
Controller Joystick Hold Delay Logic? 1 Answer
Reset Input.GetAxis Back to 0 0 Answers