- Home /
Why is Input.GetAxisRaw() not returning whole numbers when using a joystick?
So I need to only receive -1, 0, or 1 as values from the Joystick (using a wired 360 controller at the time). When I use the following code with a keyboard it works exactly as intended, but when I use the thumbstick on the gamepad I get odd movement behaviors. When I tried adding Debug.Log(Input.GetAxisRaw("Vertical"); the console showed it returning values between -0.2 and 0.2. I have no idea what is causing this and while I can think of a way around it it'll take quite a bit of hard coding I'd rather avoid if at all possible.
float previousAxisStateX;
float previousAxisStateY;
float tileSizeX = 2f;
float tileSizeY = 1f;
Vector3 transPos;
void Update()
{
if (Mathf.Abs(Input.GetAxis("Horizontal")) > Mathf.Abs(Input.GetAxis("Vertical")))
{
if (previousAxisStateX == 0)
{
transPos = new Vector3(transform.position.x + (tileSizeX * Input.GetAxisRaw("Horizontal")),transform.position.y,transform.position.z);
transform.position = transPos;
}
}
else
{
if (previousAxisStateY == 0)
{
transPos = new Vector3(transform.position.x, transform.position.y + (tileSizeY * Input.GetAxisRaw("Vertical")),transform.position.z);
transform.position = transPos;
}
}
previousAxisStateX = Input.GetAxis("Horizontal");
previousAxisStateY = Input.GetAxis("Vertical");
}
For an analog device, you should be getting values between -1 and 1. You will get a range of values, not just the integers -1, 0, and 1.
Thanks. I was able to fix it by adding
int axis = 0;
if (Input.GetAxis("Horizontal") > 0)
axis = 1;
if (Input.GetAxis("Horizontal") < 0)
axis = -1;
to my code. As for why it was returning values between -0.2 and 0.2 that was because of my
if (previousAxisStateX == 0)
Answer by Phaizran · Apr 04, 2014 at 02:47 AM
I was misunderstanding how Input.GetAxisRaw() works. Its doesn't only return whole numbers it merely removes smoothing so an analog stick that can be held at anywhere between -1 and 1 would return values as such. In then end I changed my code to
float previousAxisStateX;
float previousAxisStateY;
float tileSizeX = 2f;
float tileSizeY = 1f;
Vector3 transPos;
void Update()
{
if (Mathf.Abs(Input.GetAxis("Horizontal")) > Mathf.Abs(Input.GetAxis("Vertical")))
{
if (previousAxisStateX == 0)
{
int axis = 0;
if (Input.GetAxis("Horizontal") > 0)
axis = 1;
if (Input.GetAxis("Horizontal") < 0)
axis = -1;
transPos = new Vector3(transform.position.x + (tileSizeX * axis),transform.position.y,transform.position.z);
transform.position = transPos;
}
}
else
{
if (previousAxisStateY == 0)
{
int axis = 0;
if (Input.GetAxis("Vertical") > 0)
axis = 1;
if (Input.GetAxis("Vertical") < 0)
axis = -1;
transPos = new Vector3(transform.position.x, transform.position.y + (tileSizeY * axis),transform.position.z);
transform.position = transPos;
}
}
previousAxisStateX = Input.GetAxis("Horizontal");
previousAxisStateY = Input.GetAxis("Vertical");
}
Thanks for the idea, it was useful to me. I've made my own version of the code:
Vector3 direction = Vector3.down;
Vector3 movement = Vector3.zero;
void Update () {
// input movement
movement = new Vector3 (
Input.GetAxisRaw ("Horizontal"),
Input.GetAxisRaw ("Vertical"),
0f);
// facing
if (movement != Vector3.zero)
CalculateDirection();
}
void CalculateDirection(){
int xaxis = 0;
int yaxis = 0;
if ($$anonymous$$athf.Abs(movement.x) > $$anonymous$$athf.Abs(movement.y))
xaxis = movement.x > 0 ? 1 : -1;
else
yaxis = movement.y > 0 ? 1 : -1;
direction = new Vector3(1f * xaxis, 1f * yaxis, 0f);
}
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Making a bubble level (not a game but work tool) 1 Answer
Move a Person to a point / Repeat a function called from another script? 1 Answer
C# sticking to Cube corners position (raycasting AI script) 1 Answer
Problem with player movements 1 Answer