Problem switching between controller and mouse input

Hi guys,

In my game, you can choose between using a gamepad for your pitch and roll inputs (it’s a flight game) and using a mouse. The code for setting the input type works just fine, so my problem is something else. Here’s the relevant code:

`
...
            roll = Input.GetAxis("Roll") * (Time.deltaTime * rollSpeed) * sensitivity;
    		pitch = Input.GetAxis("Pitch") * (Time.deltaTime * pitchSpeed) * sensitivity;
    		yaw = Input.GetAxis("Yaw") * (Time.deltaTime * yawSpeed) * sensitivity;
    		
    		if (controlType == "Mouse")
			{
				roll = Input.GetAxis("Mouse X") * (Time.deltaTime * rollSpeed) * sensitivity;
    			pitch = Input.GetAxis("Mouse Y") * (Time.deltaTime * pitchSpeed) * sensitivity;
			}
    ...
    `
    

When the input is set to mouse input, for some bizzare reason, the mouse only controls only control the player’s pitch. Switching the axis in the code so that Mouse Y controls roll and Mouse X controls pitch (the opposite of how it should be) doesn’t solve the problem, since the mouse still controls only the pitch. I don’t think it has to do with anything else in the code, since the player can roll just fine when using a gamepad. In fact, gamepad controls work perfectly. It’s baffling, to say the least.

MachCUBED

Here’s what I think it is: GetAxis for something like a joystick will give you a displacement value between -1 and 1. According to the documentation, Mouse X and Mouse Y instead give you a delta of mouse movement for that frame, which may require a different formula. I suspect the way you are using the roll value doesn’t work with mouse deltas (integers): you are getting multiples of an integer instead of a fraction. For example, if you rotated (360 degrees * roll) then it would look like you were not rotating at all.

Other possibilities: double-check the inputs as defined in the Input Manager. While Mouse X and Mouse Y are given to you by default, I believe they can be edited.

I also note that in the code you posted, yaw gets set regardless of whether mouse or controller is being used. The other two values (pitch and roll) get overwritten by the mouse-driven code, but yaw isn’t. So I’m wondering if yaw is interfering with the movement when controlType == “Mouse”.

Hope this helps…