I’m trying to move an object via the analog stick on my gamepad using the following code:
public float moveSpeed = 1.5f;
public float rotateSpeed = 1.5f;
public float forwardMovement;
public float turnMovement;
public float currentSpeed;
// Update is called once per frame
private void Update()
{
forwardMovement = Input.GetAxisRaw("Vertical");
turnMovement = Input.GetAxisRaw("Horizontal");
if (forwardMovement > 0.3 || forwardMovement < -0.3)
{
rigidbody.velocity = Quaternion.AngleAxis(transform.rotation.eulerAngles.x, -Vector3.up) * -transform.up * forwardMovement * moveSpeed;
rigidbody.angularVelocity = Vector3.up * turnMovement * rotateSpeed;
}
currentSpeed = rigidbody.velocity.x;
}
When using the keys W/S everything works fine which means that my object is moving with the specified speed in the given direction. However, when I try to use the analog stick on my gamepad the object is starting to wildly move around.
The reason is that when I don’t touch the analog stick (thus the stick is in its default position at the center) GetAxis returns a value of -1. Therefore the object moves backwards. The same applies to the Horizontal axis. My assumption is that when GetAxis returns -1…1 then it means that when the stick is at the center it returns 0. -1 in case it is in one direction an +1 for the other direction. But currently it returns +1 for both directions and -1 when the stick is in the center.
I didn’t change the input settings. Apart from the code above I do not have any more code. I just started this project. I also tested this with a second controller and two Unity versions (Unity 4.3.4 and 4.5.3) and the result is always the same.
Do I miss something obvious here? Is my assumption wrong?