- Home /
Problems with horizontal input. 0 and -1 both= false.
My object rotates and transform perfectly when I use getkey and press right and left. I wanted to switch to Unity's input feature so I could have the novelty of using a 360 controller. So choosing the Horizontal, I made it so that positive=right and negative=left. As I read in the reference. The horizontal axis will give from -1 to 1 and 0 is neutral. When I run this, My object starts turning left. Which means its already false which suggests that 0=false. And since Getbutton is giving me a boolean, I am not sure how I can have the game know the difference between no input and negative input. Do I have to adjust the sensitivity? To clarify, the commented out getkeys work and I am sure I could just add an "or" to each if statement and find and input the joy button for the 360 controller but that is not elegant.
if(Input.GetButton("Horizontal")==true)
//if(Input.GetKey("right"))
{
//if(!Input.GetKey("left"))
//if(Input.GetButton("Horizontal")=true)
{
forwardv.x=(Mathf.Cos(-0.1f)*temp2.x)-(Mathf.Sin(-0.1f)*temp2.y);
forwardv.y=(Mathf.Sin(-0.1f)*temp2.x)+(Mathf.Cos(-0.1f)*temp2.y);
transform.Rotate(0,0,(float)(-0.1*180/Mathf.PI));
}
}
//if(Input.GetKey("left"))
if(Input.GetButton("Horizontal")==false)
{
//if(!Input.GetKey("right"))
//if(Input.GetButton("Horizontal")!=true)
{
forwardv.x=(Mathf.Cos(0.1f)*temp2.x)-(Mathf.Sin(0.1f)*temp2.y);
forwardv.y=(Mathf.Sin(0.1f)*temp2.x)+(Mathf.Cos(0.1f)*temp2.y);
transform.Rotate(0,0,(float)(0.1*180/Mathf.PI));
}
}
Answer by robertbu · Jun 26, 2013 at 06:20 PM
The code here is obviously wrong because you are comparing Input.GetButton("Horizontal") to 'true' rather than using values in the -1 to 1 range. Assuming you don't want a range of values, but just true/false, just do something like:
if(Input.GetButton("Horizontal") < -.1)
If you are using a joystick and your character is walking with a dead stick, consider upping the Dead setting for "Horizontal". Go to Edit/Project Settings Input.
unfortunately I cannot compare a bool with a double. Alternatively, I used GetAxis and applied something close to what you suggest and it worked beautifully on my controller but it ruined my keyboard controls. If i press left on the keyboard the object rotates indefinitely in that direction. I am thinking I need to set the float variable that holds the value from GetAxis to 0 at some point to stop the permanent spinning but I am not sure where in the above code to apply it.
It is difficult to figure out your problem without seeing the code. Post your revised code as a comment or revise the code in your original question.
Axis is a float that I store the axis information into. I guess because the controller analog has a deadzone (which equals zero) and allows for ideal object movement. But how do I tell the right and left buttons to return to zero after use.
if(axis>0)
//if(Input.Get$$anonymous$$ey("right"))
{
//if(!Input.Get$$anonymous$$ey("left"))
// if(Input.GetButton("Horizontal")=true)
{
forwardv.x=($$anonymous$$athf.Cos(-0.1f)*temp2.x)-($$anonymous$$athf.Sin(-0.1f)*temp2.y);
forwardv.y=($$anonymous$$athf.Sin(-0.1f)*temp2.x)+($$anonymous$$athf.Cos(-0.1f)*temp2.y);
transform.Rotate(0,0,(float)(-0.1*180/$$anonymous$$athf.PI));
}
}
//if(Input.Get$$anonymous$$ey("left"))
if(axis<0)
{
//if(!Input.Get$$anonymous$$ey("right"))
//if(Input.GetButton("Horizontal")!=true)
{
forwardv.x=($$anonymous$$athf.Cos(0.1f)*temp2.x)-($$anonymous$$athf.Sin(0.1f)*temp2.y);
forwardv.y=($$anonymous$$athf.Sin(0.1f)*temp2.x)+($$anonymous$$athf.Cos(0.1f)*temp2.y);
transform.Rotate(0,0,(float)(0.1*180/$$anonymous$$athf.PI));
}
}
axis=Input.GetAxis("Horizontal");
Your answer

Follow this Question
Related Questions
Adapting Horizontal and Vertical movement to New Input System? 0 Answers
How can I make GUI button send an input message when I press it? 0 Answers
The value of Input.GetAxis("Horizontal") gets stuck when Xbox controller is disconnected... 0 Answers
changing a float number by moving arrows (horizontal and vertical input)? 1 Answer
Horizontal Input showing random decimal when not pressing anything 0 Answers