- Home /
How to get smooth rotation input from gamepad thumb-stick?
I am trying to write a input script for a twin-stick shooter that takes input from a gamepad on PC or Mac. When I rotate the thumb-stick 360 degrees the input returned rotates smoothly except at the axes (and sometimes the diagonals) where it snaps to the axes or diagnals.
I've attached a minimal project that exhibits the problem. Rotate the left thumb-stick slowly and you'll see the input vector snap to the axes. This is the relavant code:
public class ThumbStick : MonoBehaviour
{
Vector3 thumbStick;
void Update ()
{
thumbStick = new Vector3(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"), 0);
//thumbStick = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0);
}
void OnDrawGizmos ()
{
Gizmos.color = Color.blue;
Gizmos.DrawWireSphere(transform.position, 1f);
Gizmos.color = Color.red;
Gizmos.DrawRay (transform.position, thumbStick.normalized);
Gizmos.color = Color.green;
Gizmos.DrawRay(transform.position, thumbStick);
}
}
I've tried:
Using both Input.GetAxis and Input.GetAxisRaw
Adjusting the Sensitivity and Type settings of the Horizontal and Vertical axes in the InputManager
A Logitech Gamepad F310 on PC and Mac
A PS3 controller on Mac
Is this something inherent in gamepad input that needs to be programmed around, or is Unity adjusting the data somehow (I could see how the snapping would be useful in other games)?
I have exactly the same problem with a xbox 360 pad, using almost the same code with a "deadzone" :
shootDirection = new Vector2(Input.GetAxis("HorizontalAim"), -Input.GetAxis("VerticalAim"));
if (shootDirection.magnitude < shootData.shootDirectionDeadZone)
{
shootDirection = Vector2.zero;
}
shootDirection.Normalize();
I remember I didn't have this issue on XNA (I migrated my project from XNA to Unity 3D). If someone have an idea, I could help a lot!
$$anonymous$$aybe if you try to make the move with a vector3.Lerp between the current position and the one you want to reach.
In this case, it's not about moving from an angle pto another but go directly to the angle given by the stick. And it's this angle returned by the stick which seems "snapped" around the 45° directions...
Answer by Aldwin · Jun 11, 2013 at 12:08 PM
I just resolved my problem going into the Input setting and lower the "sensivity" value of the stick's x and y axis. It was set to 3 in my case and setting it to 1 just worked fine for me. All is smooth now !
I was hopeful with this one, but it still sticks on the axes.
Answer by zombience · Jun 10, 2013 at 02:39 PM
try something along these lines: I haven't tested this code out (written from memory), but I'm using similar code in my movement scripts and it's basically like this.
float deltaX; float deltaY;
float sensitivityX;
float sensitivityY;
void Start()
{
}
void Update()
{
deltaX = Input.GetAxis("horizontal") * sensitivityX;
deltaY = Input.GetAxis("vertical") * sensitivityY;
Vector3 targetRotation = new Vector3(deltaX, deltaY, transform.rotation.eulerAngles.z);
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(targetRotation), .1f);
Answer by dejafou · Jul 04, 2013 at 08:29 PM
Ok, not sure what the protocol is for answering your own question, but here's what resolved my problem: In the input manager there are two horizontal and two vertical axes. One set is for keyboard and the other is for the joystick input. I was changing the settings for the keyboard input. Womp womp.
Your answer
Follow this Question
Related Questions
Using Xbox gamepad triggers as keys instead of axis? 1 Answer
horizontal menu + gamepad 1 Answer
Mapping multiple controllers 1 Answer
link gamepad to player 0 Answers