- Home /
If Statement Triggers No Matter What!
I am working on a 3d platformer and I would like the player to be able to rotate their camera around the character and I have code that can do that but in my if statement to check for the movement of the (right) joystick it triggers no matter what numbers I put in as long as they are within the range of -1 to 1 which is the joysticks range. Thank you for any help that you may provide.
//rotation
if (Input.GetAxis("HorizontalRightStick") < -0.2)
{
transform.Rotate(-Vector3.up * RotateSpeed * Time.deltaTime);
}
else if (Input.GetAxis("HorizontalRightStick") > 0.2)
{
transform.Rotate(Vector3.up * RotateSpeed * Time.deltaTime);
}
I can pretty much absolutely guarantee you that both halves of that if() statement don't execute. What makes you think they are?
When I have the code segment in, the camera will spin in circles except when I am pressing it to the right in which case it spins to the right like it is supposed to. If I comment out the code contents of the first if statement it does not do anything and if I put a print into it then it prints that statement as well.
I copied your code into Unity but I am unable to recreate your issue. This code works fine for me. Very weird indeed! :o
Ah, I'm not using joystick axis, I'm sorry - I am unable to fully troubleshoot without one then. :(
Answer by nadhimali · Jul 27, 2015 at 03:56 AM
try adding else to stop the rotation if the axis inputs not within -0.2 and 0.2
//rotation
if (Input.GetAxis("HorizontalRightStick") < -0.2)
{
transform.Rotate(-Vector3.up * RotateSpeed * Time.deltaTime);
}
else if (Input.GetAxis("HorizontalRightStick") > 0.2)
{
transform.Rotate(Vector3.up * RotateSpeed * Time.deltaTime);
}
else {
transform.Rotate(0,0,0);
}